MATLAB Answers

How do I make my variables save from my function?

3 views (last 30 days)
Sarah Johnson
Sarah Johnson on 19 Feb 2020
Commented: Sarah Johnson on 19 Feb 2020
I have written a function function [f, grad, gradTwo] = Rosen(x), which returns the Rosenbrock function value f(x) to f, its gradient vector ∇f(x) to grad, and its Hessian matrix ∇f(x) to gradTwo, at the given input point x and verify the function returns correct values.
I have written a function that does so, using x = [1, 1], but my values don't store.
function [f, grad, gradTwo] = Rosen(x)
g = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
f = g(x);
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];
gradTwo = [800*x(1)+2, -400*x(1); -400*x(2), 200];
Then my output when I run this is:
ans =
I believe my professor wants it to look like this:
f =
grad =
gradTwo =
802 -400
-400 200
Is there a way to accomplish this? Any advice would be great, I'm not that great at MATLAB


Sign in to comment.

Accepted Answer

Hiro on 19 Feb 2020
You're doing well!
Try this:
x = [1 1];
[f, grad, gradTwo] = Rosen(x)
You should prepare the three variables that recieve the calculus!

More Answers (0)