Write a MATLAB Mfile to compute the double integral below us
Write a MATLAB M-file to compute the double integral below using the composite trapezoidal rule with h = 0.1 in both the x- and y-directions. You may use the MATLAB built-in function \"trapz\" (or the \"trap.m\" discussed in class and available Canvas) in your M-file. Check your answer using the \"dblquad\" function. Provide a printout of your M-file and a printout of the command window showing your results.
Solution
Code
%with trapz
x = 0:0.1:3;
y = 0:0.1:2;
[X,Y] = meshgrid(x,y);
F = X.^4 - 3.*X.*Y + 6.* Y.^2;
I = trapz(y,trapz(x,F,2));
disp([\'Integration using trapz: \', num2str(I)]);
%with dblquad
F = @(X,Y)X.^4 - 3.*X.*Y + 6.* Y.^2;
Q = dblquad(F,0,3,0,2);
disp([\'Integration using dblquad: \', num2str(Q)]);
Output
Integration using trapz: 118.44
Integration using dblquad: 118.2
