corrected trapezoid rule function code

5 次查看(过去 30 天)
I need help on matlab coding!!
so assignment given is to use trapezoid rule, midpoint rule and corrected trapezoid rule to plot log vs log error plot here is what i have so far
This is the main script function:
a = 0;
b = 2;
%take the integral of the given function
Int = exp(2^2-1)- exp(0^2-1);
%preallocate the estimates and errors
trap_value = zeros(1,12);
mid_value = zeros(1,12);
correctedtrap_value = zeros(1,12);
trap_error = zeros(1,12);
mid_error = zeros(1,12);
correctedtrap_error = zeros(1,12);
%number of steps given in the problem (n)
k = 1:1:12;
n = 2.^k;
%repeat the value of n
for j = 1:12
trap_value(j) = traprule(a,b,n(j));
mid_value (j) = midpoint(a,b,n(j));
correctedtrap_value (j) = correctedtrapezoid (a,b,n(j));
trap_error(j) = abs(Int-trap_value(j));
mid_error = abs(Int-mid_value(j));
correctedtrap_error = abs(Int-correctedtrap_value(j));
end
figure(1)
plot(log(n),log(trap_error),log(n),log(mid_error),log(n),log(correctedtrap_error))
legend('Three rules estimate log(error) vs log(number of steps)','Location','best')
xlabel('log(n)')
ylabel('log(error)')
This is the given function to evaluate:
function [f] = func_main(x)
%this function will calculate 2xexp(x^2-1) and given x
f = 2*x*exp(x^2-1);
end
This is the trapeziod rule:
function [I] = traprule(a,b,n)
%Function called: func_main
%initiate the value for h
h = (b-a)/n;
%preallocate the output
I = 0;
for j=1:n
%left point of j trapezoid
left = a+h*(j-1);
%right point of trapezoid
right = a+h*j;
%apply the main fucntion
fleft = func_main(left);
fright = func_main(right);
%size the j trapezoid
trap = (fleft+fright)*h/2;
%add to continue the estimate
I = I+trap;
end
end
HERE IS WHERE I NEED YOUR HELP!!!!!!
so given the equation of CORRECTED TRAPEZOID RULE equation, I have to use equation to get proper value for corrected trapezoid rule.(which is attached a picture)
so Tn(f), I am guessing is the value i got from trapezoid rule function, where i already made the function file for, now i need to code to get T^c n (f). Only problem is that I am really struggling how to start the function file for CORRECTED TRAPEZOID RULE. Please help me how to write corrected trapezoid rule that can link up to the main script so i can plot out the log graph! the detail and sample coding will be a great help!

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by