function f = myfunc(x,a,b) f = (x-a).^2 + b; Please supply the code. Thank you!
ID: 3545954 • Letter: F
Question
function f = myfunc(x,a,b)
f = (x-a).^2 + b;
Please supply the code.
Thank you!
In this exercise you will be using fminsearch and function handles to find the minimum value of a function called myfunc(x,a,b) with respect to the variable x. For a=l and b=2, evaluate the function for x values from - 1 to 2 in increments of 0.1. ANSWERS: Save this as a column vector in A7.dat. Use fminsearch to find the x value that minimizes the function for a=3 and b=-3. Use an initial guess of x=0. Also evaluate the function at this x. ANSWERS: Save these two values as a row vector in A8.dat.Explanation / Answer
%% a part
x=-1:0.1:2;
a= 1; b=2;
f=(x-a).^2 +b;
f=f';
save('A7.dat', 'f' , '-ASCII')
%% b Part
a= 3; b=-3;
f = @(x)((x-a).^2 +b);
[x,fval] = fminsearch(f,0);
save('A8.dat', 'x' ,'fval', '-ASCII')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.