a) Write a MATLAB function that computes the Absolute Relative Error between two
ID: 3788303 • Letter: A
Question
a) Write a MATLAB function that computes the Absolute Relative Error between two functions. The functions, for which the error is being computed, are to be passed into the function you are creating. A Template for the function call is below:
function [Error,x,y1,y2] = AbsRelativeError( NumberOfPoints, ... StartingValue, StoppingValue, ... Function1, Function2 )
%
% [Error,x,y1,y2] = AbsRelativeError( NumberOfPoints, StartingValue,... StoppingValue, Function1, Function2 );
%
% Computes the absolute relative error, between two functions.
% The error is computed at N incremental steps between Start and Stop.
% Inputs: NumberOfPoints - Number of points to be compared. StartingValue - Starting point for the comparison. StoppingValue - Stopping point for the comparison. Function1 - a function (i.e. @sin ) for comparison. Function2 - the second function in the comparison, also assumed to be the more accurate of the two functions
% Outputs: Error - the ARE between the two functions. x - the values where the comparisons occurred. y1 - values of Function1 at x ( Function1(x) ). y2 - values of Function2 at x ( Function2(x) )
%
b) Use this function to compute the error in the approzimation of sin(x)=x for |x|<pi/4. Your program should compute sin(x) and the ARE of sin(x) vs x, for about 500 points between -pi/4 to pi/4. The program should search this data to determine the range of x values where the ARE (|sin(x)-x| / |sin(x)|) is less that 0.1 percent or 0.001.
c) Save the values computed for x, sin(x) and ARE into a file, and plot this data. This requires multiple plotes, all with different scales and views, to properly display the x and sin(x) relationship. Be sure to indicate the error bound (0.001) on any plot that contains ARE.
Explanation / Answer
clear all;
close all;
clc
x = -pi/4:0.01:pi/4 ;
y1 = sin(x)
y2 = sin(x)-x
are = abs(y2) / abs(y1) %to find absoluterelative error
if(are < 0.1)
disp(x)
end
save ('absolue.mat','y1','y2','are')
plot(y1,y2)
xlabel('y1')
ylabel('y2') , grid on
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.