Help with writing MATLAB code - Consider the function $f(x) = x \cos(-x^2)$

1.1k Views Asked by At

Would anyone be able to help me with this question?

Consider the function:

$f(x) = x \cos(-x^2)$

Write MATLAB functions f.m and fp.m for the function $f$ and its derivative $f '$, respectively.

1

There are 1 best solutions below

0
On BEST ANSWER

for the function

function y=f(x)
     y=x*cos(-x^2);
end

and for its derivative

function dy=df(x)
     dy=cos(x^2)-sin(x^2)*2*x^2;
end

if your inputs are vectors then you should change the codes as follows

function y=f(x)
     y=x.*cos(-x.^2);
end

and for its derivative

function dy=df(x)
     dy=cos(x.^2)-sin(x.^2).*2*x.^2;
end