I am receiving an error message that there are not enough input arguments in my function f.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
function [f, g] = rosenbrock(x)
f = sum((1 - x(1:end-1)).^2 + 100*(x(2:end) - x(1:end-1).^2).^2);
g = zeros(size(x));
g(1) = -400*x(1)*(x(2) - x(1)^2) - 2*(1 - x(1));
g(2:end-1) = 200*(x(2:end-1) - x(1:end-2).^2) - 400*x(2:end-1).*(x(3:end) - x(2:end-1).^2) - 2*(1 - x(2:end-1));
g(end) = 200*(x(end) - x(end-1)^2);
end
function [f, g] = exponential(x)
f = -exp(-0.5*sum(x.^2));
g = -x.*exp(-0.5*sum(x.^2));
end
2 Kommentare
Antworten (2)
Walter Roberson
am 6 Mär. 2023
Looks to me as if it works
format long g
x = randn(1,10)
[f1, g1] = rosenbrock(x)
[f2, g2] = exponential(x)
function [f, g] = rosenbrock(x)
f = sum((1 - x(1:end-1)).^2 + 100*(x(2:end) - x(1:end-1).^2).^2);
g = zeros(size(x));
g(1) = -400*x(1)*(x(2) - x(1)^2) - 2*(1 - x(1));
g(2:end-1) = 200*(x(2:end-1) - x(1:end-2).^2) - 400*x(2:end-1).*(x(3:end) - x(2:end-1).^2) - 2*(1 - x(2:end-1));
g(end) = 200*(x(end) - x(end-1)^2);
end
function [f, g] = exponential(x)
f = -exp(-0.5*sum(x.^2));
g = -x.*exp(-0.5*sum(x.^2));
end
0 Kommentare
Image Analyst
am 7 Mär. 2023
You're probably just clicking the green run triangle button. Doing that does not "invent" some x to pass in to the function. You have to define x somehow and then call your rosenbrock function from the command line or a script, like
x = rand(1,10);
[f, g] = rosenbrock(x);
0 Kommentare
Siehe auch
Kategorien
Find more on Pole and Zero Locations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!