MATLAB Answers

corrected trapezoid rule function code

조회 수: 30(최근 30일)
PetronasAMG
PetronasAMG 3 Dec 2017
댓글: Jan 4 Feb 2018
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!

  댓글 수: 1

로그인 to comment.

답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by