In: Electrical Engineering
1)
a) Write a MATLAB function called Area1 having two inputs, r and N, and an output, A1. The output A1 should be the area under a curve, f(x), for x starting at x_start and ending at x_end. The input r should be a vector (array) having x_start and x_end as its two elements. The input N should be an integer equal to the number of equallength sub-intervals in which the interval from x_start to x_end should be divided. Here, we will use function: f(x) = 0.1x 2 + cos(0.4 ? x) +exp(-x 2 /10) for x_start = 0 and x_end = 10. In other words, the Area1.m file which includes the function should look as follows:
function A1 = Area1(r, N)
……. Code needed to calculate the area…..
end
To compute the approximate area, implement the midpoint approximation method in MATLAB. In other words, the area will be approximately equal to the sum of the rectangular areas.
(b) Repeat part (a), but call the function Area2, and the corresponding MATLAB file ‘Area2.m’, and instead of using the midpoint approximation method, use the trapezoid method.
(c) Create another script (e.g., main.m) and call A1 = Area1(r, N); and A2 = Area2(r, N); within a for loop which increases N from 5 to 50 with step of 1. Save the 46 results into vectors A_all1 and A_all2. Then plot A_all1 and A_all2 with respect to the 46 corresponding values of N in a single plot, and observe how they converge to the true area as N increases. Add appropriate xlabel and ylabel. Which method converges faster?
Dear user,
Please find the solution
Mid point Approximation:
The above formula is used in matlab code.
Trapezoidal rule:
This formula is used in matlab code.
matlab code main file:
%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
close all;
%%
r=[0 10];
i=1;
for N=5:50
area1(i)=Area1(r,N);
area2(i)=Area2(r,N);
i=i+1;
end
%%
subplot(2,1,1);
plot(5:50,area1,'linewidth',1.5);
title('Mid point Approximation');
subplot(2,1,2);
plot(5:50,area2,'linewidth',1.5);
title('Trapezoidal Rule');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function files:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function A1=Area1(r,N)
x_start=r(1);
x_end=r(end);
step=(x_end-x_start)/N;
steps=x_start:step:x_end-step;
f=@(x) 0.1*x^2+cos(0.4*pi*x)+exp(-(x^2)/10);
ar=0;
for i=1:length(steps)-1
ar=ar+step*f((steps(i)+steps(i+1))/2); % Mid point
Approximation
end
A1=ar;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function 2:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function A2=Area2(r,N)
x_start=r(1);
x_end=r(end);
step=(x_end-x_start)/N;
steps=x_start:step:x_end-step;
f=@(x) 0.1*x.^2+cos(0.4*pi*x)+exp(-(x.^2)/10);
ar=2*f(steps);
A2=(step/2)*(sum(ar)-ar(1)-ar(end)); % Trapezoidal Method
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Output:
By noticing both of them, we can see that the Mid point approximation method converges faster than the Trapezoidal method.
Please let me know if you have any doubts
Thank You.