In Matlab Compare the accuracy of the brute force method wit
In Matlab
Compare the accuracy of the \"brute force\" method with the nested polynomial method (\"Horner\'s method\") for evaluating polynomial expansions. Given the polynomial. PN(X) = sigma_i=0^N a_i x^i, The \"brute force\" method pseudo-code is y = 0. 0; for (int i=0;i x^{i}} The Homer\'s method pseudo code is y=a_{N}; for (int i=N;i >= 1; i--) {y=y*x+a_{i-1}} Compare these methods for the Taylor series expansion of log(1 + x); especially study the respective performances for |x|Solution
clc;clear;
 N = 200; % Number of terms considering in the series
 x = 1;% x value
 a(1:2:N) = 1./(1:2:N); % coefficients of polynomial
 a(2:2:N) = -1./(2:2:N);% coefficients of polynomial
 % brute force method
 y = 0.0;
 for i = 1:N
     y = y+a(i)*x^(i);
 end
 fprintf(\'Using brute force method log(1+%f) = %f\ \',x,y);
 % Horner\'s method
 y = a(N);
 for i =N:-1:2
     y = y*x+a(i-1);
 end
 fprintf(\'Using Horners method log(1+%f) = %f\ \',x,y);
 % Matlab result
 fprintf(\'Using matlab function log(1+%f) = %f\ \',x,log(1+x));
Output
Using brute force method log(1+1.000000) = 0.645635
 Using Horners method log(1+1.000000) = 0.645635
 Using matlab function log(1+1.000000) = 0.693147
 >>
Matlab code to compare accuracy of numerical derivative approx at x =1
clear all;clc;
 f = @(x) (x.^4).^(1/3); % The given function
 df = @(x) (4/3)*(x^(1/3)); % the derivative of given function
 % Different formula for computing the derivative numberically
 M1 = @(x,h) (f(x+h)-f(x))./h;
 M2 = @(x,h) (f(x+h)-f(x-h))./(2*h);
 M3 = @(x,h) (-f(x+2*h)+4*f(x+h)-3*f(x))./(2*h);
 M4 = @(x,h) (f(x-2*h)-4*f(x-h)+3*f(x))./(2*h);
 % various values of h
 h = 0.0000000001:0.000000001:0.005;
 x = 1; % For x = 1
 % Computing the error
 M1_error = abs(M1(x,h)-df(x));
 M2_error = abs(M2(x,h)-df(x));
 M3_error = abs(M3(x,h)-df(x));
 M4_error = abs(M4(x,h)-df(x));
 % Finding the index of minimum error location
 [~,i1] = min(M1_error);
 [~,i2] = min(M2_error);
 [~,i3] = min(M3_error);
 [~,i4] = min(M4_error);
 % printing the h crit
 fprintf(\'For M1 h crit = %0.10f\ \',h(i1));
 fprintf(\'For M2 h crit = %0.10f\ \',h(i2));
 fprintf(\'For M3 h crit = %0.10f\ \',h(i3));
 fprintf(\'For M4 h crit = %0.10f\ \',h(i4));
 Output
For M1 h crit = 0.0000000161
 For M2 h crit = 0.0000059481
 For M3 h crit = 0.0000042871
 For M4 h crit = 0.0000042871
 >>
Matlab code to compare accuracy of numerical derivative approx at x =0
clear all;clc;
 f = @(x) (x.^4).^(1/3); % The given function
 df = @(x) (4/3)*(x^(1/3)); % the derivative of given function
 % Different formula for computing the derivative numberically
 M1 = @(x,h) (f(x+h)-f(x))./h;
 M2 = @(x,h) (f(x+h)-f(x-h))./(2*h);
 M3 = @(x,h) (-f(x+2*h)+4*f(x+h)-3*f(x))./(2*h);
 M4 = @(x,h) (f(x-2*h)-4*f(x-h)+3*f(x))./(2*h);
 % various values of h
 h = 0.0000000001:0.000000001:0.005;
 x = 0; % For x = 0;
 % Computing the error
 M1_error = abs(M1(x,h)-df(x));
 M2_error = abs(M2(x,h)-df(x));
 M3_error = abs(M3(x,h)-df(x));
 M4_error = abs(M4(x,h)-df(x));
 % Finding the index of minimum error location
 [~,i1] = min(M1_error);
 [~,i2] = min(M2_error);
 [~,i3] = min(M3_error);
 [~,i4] = min(M4_error);
 % printing the h crit
 fprintf(\'For M1 h crit = %0.10f\ \',h(i1));
 fprintf(\'For M2 h crit = %0.10f\ \',h(i2));
 fprintf(\'For M3 h crit = %0.10f\ \',h(i3));
 fprintf(\'For M4 h crit = %0.10f\ \',h(i4));
 Output
For M1 h crit = 0.0000000001
 For M2 h crit = 0.0000000001
 For M3 h crit = 0.0000000001
 For M4 h crit = 0.0000000001
 >>
The minimum error for x = 0 is at the smallest step size itself


