The problem must be solved using MATLAB. It should have is own program and plots
ID: 3925072 • Letter: T
Question
The problem must be solved using MATLAB. It should have is own program and plots must be formatted appropriately. Codes should write the output to a file (this need not be the same file).
An object starts with an initial velocity of 3m/s at t =0s and accelerates a(t) = 7t m/s2. Use the trapezoid rule and the Simpson’s rule to find the total distance the object travels in 4s. Use 10 equally spaced intervals and 100 equally spaced intervals. Compare the results of the trapezoid rule and the Simpson’s rule with the analytic solution (which you can do by hand with calculus). Comment on the performance of the two methods and the uses of different interval sizes.
Explanation / Answer
function Q = trapezoidal()
ezplot('x.^3', [0, pi]), hold on
Q = zeros(1,7); %vector results initializing
for j = 1:7
n = 2^j;
x = pi*(0:1/n:1);
plot(x, x.^3, 'r')
values = [1, 2*ones(1,n-1), 1];
Q(j) = pi/(2*n)*sin(x)*values';
end
return
end
function Q = simpson(fun, a, b, n)
if mod(n,2)
error('n must even')
end;
values = [1,2*ones(1,n-1)+2*mod([1:n-1],2),1] ;
x = a:(b-a)/n:b;
Q = (b-a)/(3*n)*fun(x)*values';
return
simpson(@(x) x.^3,0,1,2)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.