Question

In: Electrical Engineering

1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an...

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?

Solutions

Expert Solution

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.


Related Solutions

Write a Matlab function called: lin_interp. The function should have three inputs: the two original data...
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data arrays (call them x and f), and the array you would like to interpolate to (call it xstar). The function should have one output: the interpolated array ( call it fstar). The function should be able to interpolate x and f onto xstar using linear interpolation and give the result as fstar. The function may not use any intrinsic functions except length.
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
Write a MATLAB function named myaccel that accepts three inputs and has a single output. Inputs:...
Write a MATLAB function named myaccel that accepts three inputs and has a single output. Inputs: a list of time values a list of position values a single time value. Output: a single number which is obtained by numerically differentiating position with respect to time twice (forward difference method) and then interpolating the results based on the third input. Example: time=0:10; position=time.^3; myaccel(time,position,2.8) % should return 22.8
Perform the following tasks on R Studio/R Construct a function called conv3 which inputs a measurement...
Perform the following tasks on R Studio/R Construct a function called conv3 which inputs a measurement in centimeters and outputs the corresponding measurement in inches. However, if a negative value is entered as an input, no conversion of unit is done and an error message is printed instead.
Write a function called tokenizeTelNum that inputs a telephone number as a string in the form...
Write a function called tokenizeTelNum that inputs a telephone number as a string in the form (555) 555-5555. The function should use the function strok to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The function should convert the area code string to int and...
Write a Matlab/SciLab function that accepts inputs as degrees and computes the equivalent within the interval...
Write a Matlab/SciLab function that accepts inputs as degrees and computes the equivalent within the interval 0 to 360. function de=equivalent(d) For example, equivalent(540) = 180 equivalent(-30) = 330
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
Write a function that inputs the, n, the number of random trials for a standard normal...
Write a function that inputs the, n, the number of random trials for a standard normal distribution and then generates an array of n random observations (ie generate a random sample of n observations from a N(0,1) distribution. Then have the function perform a simple t-test on the sample, testing the null hypothesis that is mean 0. Have the function return two things: the results of the ttest and another variable that is set to the string= “reject” if the...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single...
Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single file here in Canvas. To make grading easier, please do the following: • Close your function with "end". • Don't have your function print anything to the console; just return the output. To accomplish this: o Put a semicolon ";" at the end of each statement. o Put a percent "%" before each disp or fprintf statement. The function numdiff should take the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT