Problem 1. Basics Part 1- (1 point. Instructor\'s Initials Anonymous functions a
ID: 2292802 • Letter: P
Question
Problem 1. Basics Part 1- (1 point. Instructor's Initials Anonymous functions and function handles. Recall from the lecture that the instructor wrote an anonymous function for computing future value from present value that went like this: FV- @(PVr,n) PV*(1+r)'n. What makes this an anonymous function? What is the function handle? Your turn: Write an anonymous function that computes exp(-x) In(x) Call it f. Test data: f(1)- 0, f(2) 0938. Plotting functions. Try fplot('cos'). Then try fplot('cos', [0, 2*pil). Now give 'cos' the handle fusing f-@cos. Try fplotf [0,2"pi). Create an anonymous function exp cos(10x). Plot it from 0 to 10 Note that fplot works only on functions of one variable -it would not work on the FV function above Roots of functions. Use fzero('sin' Ipi/2,3 pi/2] to find where the sin function crosses 0 on the interval between /2 and 3T/2. Now find the two zeros of g(x) 2 10x +1 in the interval between 0 and 3. You may want to graph the function first. Check your answers here: xi-0.2029, x:-2.0745. Linear interpolation. 1) Find the y-value corresponding to x 3.5 on the line between (1, 7) and (4, 3). Use a linear interpolation to do this: LetX = [1,4] and Y = [73). For the interpolation use yq - interpI(X.Y,3.5). (Ans: yq 3.66667). To see this graphically, plot(X, Y, ,3.5, yq, 'o') 2) Now redefine X-10:5] and Y = [ 15, 10, 9, 6, 2,0]; Let xq-3.5, and yq-interp 1 (X.YXQ. Then plot(X, Y, xq, yq, o'). 3) Finally, keeping X and Y from part 2), Let newX-[0:.2:5], and newY-interp1(X,Y.newX); plot(X, Y, .newx, newY, 'o'). Spline interpolation. LetX-05], Y = [ 15, 10, 9, 6, 2,0), and newX-[0:25] newYspline-interp1(X,Y,newX, 'spline'). Then plotx, Y, newX, newY spline, -o'). Linear regression. LetX-[05] and Y = [15, 10, 9, 6, 2,0]. Assume these are measured data points, so plot them as points: plot(X,Y,""). Then determine the coefficients of the linear regression of the form y mx+ b using m = polyfit(XY, 1 ). Compute Yfit m( 1 )"X m(2) (notice that m(l)-m, and m(2)- b. Plot(X,y,'* X Yfit) to show the data with the best fit line through it as shown on the right. Finally add text to the graph using gtext('y =-2.914x + 14.286). ?Polynomial regression. Let X=[0:5] and Y = [-0.9, 1.3, 5.7, 12.8, 22.2, 33.9]. Use the same approach as shown above for linear regressions to plot the best quadratic fit of the form y ax+ bx+ c through the data using polyfitx Y.2),Explanation / Answer
1)
code for fv.m
function y = fv(x)
y = exp(-x)*log(x);
end
code for main.m
clc;clear;
f = @fv;
x = 1;f
b = f(x)
x = 2;
b1 = f(x)
2)
code for test.m
function y = test (x)
y = exp(-x)*cos(10*x);
end
code for main.m
clc;clear;
figure;
fplot('cos',[0,2*pi])
f = @cos;
figure;
fplot(f,[0,2*pi])
g = @test;
figure;
fplot(g,[0,10])
3)
code for g_x.m
function y = g_x(x)
y = (2^x)^2 - 10*x + 1;
end
code for main.m
clc;clear;
syms x
fzero('sin',[pi/2,3*pi/2])
g = @g_x;
figure;
fplot(g, [0,3])
grid on
fzero('g',[0,3])
4)
clc;clear;
X = [1,4]
Y = [7,3]
yq = interp1(X,Y,3.5)
figure;
plot(X,Y,'-*',3.5,yq,'o')
X = [0:5]
Y = [15,10,9,6,2,0]
xq = 3.5;
yq = interp1(X,Y,xq)
figure;
plot(X,Y,'-*',xq,yq,'o')
newX = [0:.2:5]
newY = interp1(X,Y,newX);
figure;
plot(X,Y,'-*',newX,newY,'o')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.