Calculate and plot the power-series approximation and error of cosine for a sing
ID: 644555 • Letter: C
Question
Calculate and plot the power-series approximation and error of cosine for a single x-value and a range of x-values. The error for the approximation is defined as the absolute value of the difference between the approximation and the true value (assume Matlab's cos() function is the true value).
(a) Write the following functions:
function [approx] = cosinePowerSeries(x,n)
function[err] = cosinePowerSeriesError(x,n)
function [] = plotCosinePowerSeries(xmin,xmax,n)
function[] = plotCosinePowerSeriesError(xmin,xmax,n)
In these functions:
n: positive integer which defines the number of terms (from zero) of the approximation :positive integer which defines the number of terms (from zero) of the approximation
x: value for which you are seeking the cosine approximation (cos(x))
[xmin,xmax]:x-range of the calculated/plotted approximation
(b) Create a graphical interface (GUI) allowing display and control the power approximations.
Use the graphical user interface development environment(guide) to create a graphical user interface with the folliwing properties:
(1) space for the user to enter xmin, xmax, a specific value ofx, and n
(2) two plotting areas for the approximation and the error,
(3) a push botton to execute the plotting and calculation,
(4) text areas which display the approximation and error at the specified values of x and n.
Additionally, there should be radio buttons, which allow the user
(5) to hold the current plot so that additional plots may be overlaid, and
(6) to clear the current axes.
Calculate and plot the power-series approximation and error of cosine for a single x-value and a range of x-values. The error for the approximation is defined as the absolute value of the difference between the approximation and the true value (assume Matlab's cos() function is the true value). (a) Write the following functions: function [approx] = cosinePowerSeries(x,n) function[err] = cosinePowerSeriesError(x,n) function [] = plotCosinePowerSeries(xmin,xmax,n) function[] = plotCosinePowerSeriesError(xmin,xmax,n) In these functions: n: positive integer which defines the number of terms (from zero) of the approximation :positive integer which defines the number of terms (from zero) of the approximation x: value for which you are seeking the cosine approximation (cos(x)) [xmin,xmax]:x-range of the calculated/plotted approximation (b) Create a graphical interface (GUI) allowing display and control the power approximations. Use the graphical user interface development environment(guide) to create a graphical user interface with the folliwing properties: (1) space for the user to enter xmin, xmax, a specific value ofx, and n (2) two plotting areas for the approximation and the error, (3) a push botton to execute the plotting and calculation, (4) text areas which display the approximation and error at the specified values of x and n. Additionally, there should be radio buttons, which allow the user (5) to hold the current plot so that additional plots may be overlaid, and (6) to clear the current axes.Explanation / Answer
Matlab code:-
function [approx]=cosinePowerSeries(x,n)
approx=1;
for k=0:approx:n;
approx(k+1)=((-1)^k)*(x^(2*k))/(factorial(2*k));
end
approx=sum(approx);
end
function [err]=cosinePowerSeriesError(x,n)
err=abs(cos(x)-(cosinePowerSeries(x,n)));
end
function rs=plotCosinePowerSeries(xmin,xmax,n)
fplot(@(x) cosinePowerSeries(x,n),[xmin,xmax])
end
function rs=plotCosinePowerSeriesError(xmin,xmax,n)
fplot(@(x) cosinePowerSeriesError(x,n),[xmin,xmax])
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.