Matlab please help please MECH 1310 Homework #07-cos-Series Due: 4/19/2018 To be
ID: 3282616 • Letter: M
Question
Matlab please help please MECH 1310 Homework #07-cos-Series Due: 4/19/2018 To be collected for grade. Submit your work to the Dropbox in a file named mycosD.m before 11:59 pm on the due date. (20] The Taylor Series expansion for cosine is: cos(x) ?,nur" where x is an angle in radians. The correct definition of relative error is: realtiveError opproxValue Craft a UDF that: trueValue accepts an angle in degrees as the 1t variable in input argument list accepts a value for convergence tolerance as the 2d variable the in input argument list converts the angle from degrees to radian (x in series MUST be in radians) uses the above series to compute an approximate value for the cosine of the angle . For the purposes of this assignment, assume that the value computed by the MATLAB built-in function for cosine is the true value.) Uses the fprintf function to show in the CW in an easily red format on successive lines: - the angle in degrees, the approximate value for the cosine computed using the specified relative error tolerance, - the specified relative error tolerance, and -the number of terms of the series required to produce the approximate value. - Returns as output arguments, in the following order: - the series approximation value for the cosine, - the relative error, and -the number of terms required to compute the approximate value. Produces one plot with 5 curves, all on-2??xExplanation / Answer
MATLAB CODE:
function [ val, rel_err, n_terms ] = mycosD( angle, tol )
% angle : angle in radians
% tol : relative error desired
% val : value approximated using the series
% rel_err: relative error of the approximated value
% n_terms: number of terms required to achieve convergence
% Convert angle to radians
angle_rad = angle * pi / 180;
% Initialize the error to 10*tol to start the process
rel_err = 10 * tol;
% Keep approximating until desired tolerance is reached
val = 0; % Initialize our approximation with zero
actualValue = cos(angle_rad);
n_terms = 0; % Index
while rel_err > tol
% Update the approximation
val = val + ((-1).^n_terms ./ factorial(2*n_terms)) .* angle_rad.^(2*n_terms);
% Calculate the error
rel_err = abs(val - actualValue) / actualValue;
% Update the index
n_terms = n_terms + 1;
end
n_terms = n_terms - 1; % Decrement index to account for the last increment which is extra
% Output the values
fprintf("Angle (degrees): %f ", angle);
fprintf("Approximate Value Determined: %f ", val);
fprintf("Specified Tolerance: %f ", tol);
fprintf("Number of Terms needed: %f ", n_terms);
% Plot the figure
figure;
x = -2*pi:0.1:2*pi;
y1 = cos(x);
y2 = 1 + ((-1)^1 / factorial(2*1)) * x.^2;
y3 = y2 + ((-1)^2 / factorial(2*2)) * x.^4;
y4 = y3 + ((-1)^3 / factorial(2*3)) * x.^6;
y5 = y4 + ((-1)^4 / factorial(2*4)) * x.^8;
plot(x, y1);
hold on;
plot(x, y2);
plot(x, y3);
plot(x, y4);
plot(x, y5);
legend('Actual', '2 terms', '3 terms', '4 terms', '5 terms');
axis([-2*pi 2*pi -2 2]);
end
SAMPLE OUTPUT:
>> mycosD(30, 1e-15)
Angle (degrees): 30.000000
Approximate Value Determined: 0.866025
Specified Tolerance: 0.000000
Number of Terms needed: 7.000000