In Matlab write a function with the header function [errTrap, errMid] = myIntegr
ID: 3690880 • Letter: I
Question
In Matlab write a function with the header function [errTrap, errMid] = myIntegrationError(f, a, b, n) which compares predictions made with your myTrapRule and myMidPoint functions with Matlab’s built-in “integral” function. errTrap and errMid should be numeric percentages.
Note: use “integral(f,a,b)” instead of “quad(f,a,b)”. “integral” is apparently taking the place of “quad” in new Matlab versions going forward.
Test Case 1:
>> f1 = @(x) x.^5 + x.^3./sin(x).^4 + 1;
>> [eT, eM] = myIntegrationError(f1,1,2,10)
eT = 0.591776828444692
eM = 0.295517218724801
Test Case 2:
>> f2 = @(bob) sin(bob).*cos(bob).*exp(bob).*tan(bob);
>> [eT, eM] = myIntegrationError(f2,1,2,10)
eT = 0.074830611874473
eM = 0.037403229442031
Explanation / Answer
for i=1:length(a) fprintf('Execution of part (%s) ',Strings(i)) fprintf(' n Trapezoid Rule Simpson''s Rule Midpoint Rule ') fprintf('================================================================ ') for j = 1:length(n) fprintf('%3i %15.6f %17.6f %15.6f ', n(j), Trapezoid(f{i},a(i),b(i),n(j)), ... Simpson(f{i},a(i),b(i),n(j)), Midpoint(f{i},a(i),b(i),n(j)) ) end fprintf(' Exact solution of part (%s) = %f ',Strings(i),I(i)) fprintf(' n Error_n Trapeoid Ratio_n Trap Error_n Simps Ratio_n Simps Error_n Midpoint Ratio_n Mid ') fprintf('================================================================================================================================ ') for j=1:length(n) ErrTrap(j)=abs(I(i)- Trapezoid(f{i},a(i),b(i),n(j))); ErrSimps(j)=abs(I(i)- Simpson(f{i},a(i),b(i),n(j))); ErrMid(j)=abs(I(i)- Midpoint(f{i},a(i),b(i),n(j))); if jRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.