corrected trapezoid rule function code

조회 수: 1 (최근 30일)
PetronasAMG
PetronasAMG 2017년 12월 3일
댓글: Jan 2018년 2월 4일
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에서 Get Started with Control System Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by