I am having the worst time with this assignment. Any comment on my code would be
ID: 668538 • Letter: I
Question
I am having the worst time with this assignment. Any comment on my code would be helpful.
1) (2 pts) Write a function in MATLAB that calculates the “absolute relative error” between each point of two vectors. This file should not require loops. Helpful reminder: at each point, ARE=| (true?approximate) true |×100%
Here is what I have for this one:
2) (3 pts) The useful Taylor series of the sine function starts from the complex exponential form of sine: sin(x )= 1 2 j (e jx?e ?jx ) . From there, and the Taylor series of the exponential function, one can derive the rather clean result that sin(x )=x? x 3 3! + x 5 5! ?... , or more formally sin(x )=? i=0 ? (?1) i x 2i+1 (2i+1)! . This problem investigates the small-angle approximation to sine often used in physics – namely, that sin(x) = x.
Here is a created function that is not working:
a) Create a vector of angles (in radians) in the interval of [-?/4, ?/4], using 1000 points (hint: linspace), and another vector of their sines (using “sin”).
Here is my code so far for this:
b) Use the function from question 1 to calculate the ARE for our “approximation” for each value of x. Place the first ten values for x, sin(x), and the ARE in a table. Do NOT turn in a 1000-row table. Be sure to include the code used for this question in a script file, so you can submit it with the homework (and use parts of it in the following problem).
3) (5 pts) Using the variables from question 2 (that is, the angles, their small-angle approximations, their true sines, and the ARE for each estimate), create plots showing the following:
a) The true and approximated sine functions over the interval specified. Based on visual inspection of that plot alone, report the range where the approximation looks “good.” Report this range in both radians and degrees.
b) Absolute relative error vs angle. Include a colored horizontal line at 0.1%.
c) A close-up of the ARE vs angle that allows one to more accurately readout the area under the 0.1% threshold (perhaps a y limit of 0.2% would be helpful). Do not forget to label axes and provide figure titles. These are absolute minimums for any plot in any setting. Plot in MATLAB and submit the code to create your plots – do not use Excel or another external program.
4) (5 pts) You are offered financing for a purchase, but have forgotten the actual interest rate. The payments are $500/month for five years, and the item costs $25,000. You wish to calculate the interest rate before discussing the purchase with your spouse/significant other/parents, so that you don't embarrass yourself by admitting your lack of attention. You know the formula for annual payment A is: A=P? i(i+1) n (1+i) n?1 where P is the principal, i is the interest rate (in decimal, not percentage, so 10% is entered as 0.1), and n is the number of years. You decide, being an engineer, that you can figure this out, but algebra is letting you down. This problem will ask you to find the interest rate at various resolutions using incremental search (a terrible plan, but very easy to program).
5.a) Write a general-purpose incremental search function. This function should calculate the “x” (in this case, “i”) values of interest using equal spacing (the bounds, and the number of points, should be inputs to the function). Your search function should also take a function handle as an input. This function handle should be used to calculate the function value (A) at each point in the incremental search (do not merely pass in a series of i and A values, for example). An acceptable prototype would be: function [rootLocations, numRoots] = IncSearch(func, lowerBound, upperBound, numPoints) Decide for yourself how you wish to define numPoints (points between the boundaries or total points), but make sure you comment it appropriately.
b) Using your new function from part a, search for the interest rate using a starting bracket of 1% to 20%. Report the answer using bracket sizes of 1, 0.1, and 0.01 percentage points (you'll have to calculate the number of points required, and then call your function). Note I said resolution not e.g. ARE – I'm referring here to the distance between adjacent evaluation points on the “interest rate” axis. You'll also need to create an anonymous function using the above equation.
c) Using MATLAB's tic and toc commands (in a script file, not the command window), compare the computational complexity (time to complete) of solving this problem with each of the resolutions in part b.
Explanation / Answer
Note:
Posted question contains more than one question so please post other questions as separate question.
Solution:
1.
funxction [V]=absrelerror(T,P,OP)
assert(isequal(size(T),size(P)),'T and P must have the same
size.')
X=abs(T-P)./T);
% Code to Transform input
OP=lower(OP);
% Code to Compute metric
switch OP
% Code for Errors
case 'oe'
V1=X;
% Code to find Absolute errors
case 'ae'
Verr=errperf(T,P,'oe');
V1=abs(Verr);
% Code for Mean absolute error
case 'omae'
Vaerr=errperf(T,P,'ae');
V1=mean(Vaerr);
% Code for Squared errors
case 'ose'
Verr=errperf(T,P,'oe');
V1=Verr.^2;
% Code for Mean squared error
case 'omse'
Vserr=errperf(T,P,'ose');
V1=mean(Vserr);
% Code for Root mean squared error
case 'rmse'
Vmserr=errperf(T,P,'omse');
V1=sqrt(Vmserr);
% Code for Relative errors
case 'ore'
assert(all(T),'All elements of T must be non
zero.')
Verr=errperf(T,P,'oe');
V1=Verr./T;
% Code for Absolute relative errors
case 'oare'
Vrerr=errperf(T,P,'ore');
V1=abs(Vrerr);
% Code for Mean absolute relative error
case 'omare'
Vare=errperf(T,P,'oare');
V1=mean(Vare);
% Code for Squared relative errors
case 'osre'
Vrerr=errperf(T,P,'ore');
V1=Vrerr.^2;
% Code for Mean squared relative error
case 'omsre'
Vsre=errperf(T,P,'osre');
V1=mean(Vsre);
% Code for Root mean squared relative error
case 'ormsre'
Vmsre=errperf(T,P,'omsre');
V1=sqrt(Vmsre);
% Code for Percentage errors
case 'ope'
Vrerr=errperf(T,P,'ore');
V1=Vrerr*100;
% Code for Absolute percentage errors
case 'oape'
Vperr=errperf(T,P,'ope');
V1=abs(Vperr);
% Code for Mean absolute percentage error
case 'omape'
Vaperr=errperf(T,P,'oape');
V1=mean(Vaperr);
% Code for Squared percentage errors
case 'ospe'
Vperr=errperf(T,P,'ope');
V1=Vperr.^2;
% Code for Mean squared percentage error
case 'omspe'
Vsperr=errperf(T,P,'ospe');
V1=mean(Vsperr);
% Code for Root mean squared percentage error
case 'ormspe'
Vmsperr=errperf(T,P,'omspe');
V1=sqrt(Vmsperr);
otherwise
error('OP is invalid.')
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.