Numerical analysis Experiment with the Midpoint Rule for Int
Numerical analysis
Experiment with the Midpoint Rule for Integration, Euler\'s Method for First Order Initial Value Problems, Newton\'s Method for finding zeros of functions, and the Sparse First Difference Matrix for computing first derivative approximations
please help
Solution
f=@(x) x;
g=@(x) x.^3;
disp(\'***** question 1, part 1*****\')
n=[10 20 40 80 160];
error=zeros(1,5);
for i=1:length(n)
h=(1-0)/n(i);
x=0:h:1;
y=f(x);
result=sum(y)*h;
error(i)=abs(result-0.5);
fprintf(\'\ integration with n=%d steps is %f\ \',n(i),result);
end
fprintf(\'\ exact result is 0.5\ \');
disp(\' n error \')
table(:,1)=n\';
table(:,2)=error\';
disp(table)
disp(\'***** question 1, part 2*****\')
n=[10 20 40 80 160];
error=zeros(1,5);
for i=1:length(n)
h=(1-0)/n(i);
x=0:h:1;
y=g(x);
result=sum(y)*h;
error(i)=abs(result-0.5);
fprintf(\'\ integration with n=%d steps is %f\ \',n(i),result);
end
fprintf(\'\ exact result is 0.5\ \');
disp(\' n error \')
table(:,1)=n\';
table(:,2)=error\';
disp(table)
disp(\'****question 2 part 1*****\')
n=[10 20 40 80 160];
for i=1:length(n)
h=1/n(i);
x=0:h:1;
y0=1;
y1=y0+f(x(1))*h;
for j=2:n(i)
y0=y1;
y1=y0+f(x(j))*h;
end
fprintf(\'\ soultion for n=%d is %f.\ \',n(i),y1);
end
disp(\'****question 2 part 2*****\')
for i=1:length(n)
h=1/n(i);
x=0:h:1;
y0=1;
y1=y0+g(x(1))*h;
for j=2:n(i)
y0=y1;
y1=y0+g(x(j))*h;
end
fprintf(\'\ soultion for n=%d is %f.\ \',n(i),y1);
end

