fminunc is not working in my equation, can you help me with these errors? i am a beginner in matlab

hi.. i am trying to optimize this equation through matlab's fminunc
in the editor i typed:
function F = fun(x,e) F = -0.2662*x.^6 + 48.19*x.^5 - 3424.2*x.^4 + 121708*x.^3 - 2*e^6*x.^2 + 2*e.^7*x - 6*e.^7; end
in the command line i typed the following and at the same time got results shown:
x0= [0,0]; [x, fval] = fminunc (@fun,x0); ??? Undefined function or variable 'e'.
Error in ==> fun at 2 F = -0.2662*x.^6 + 48.19*x.^5 - 3424.2*x.^4 + 121708*x.^3 - 2*e^6*x.^2 + 2*e.^7*x - 6*e.^7;
Error in ==> fminunc at 227 f = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
can you help me fix this?

 Réponse acceptée

what are your variables in the function? x can be a vector and when plugged in F should evaluate to a scalar. By the looks of your function 'fun' that doesn't seem to be the case.

9 commentaires

actually i only have x as a variable...
i was just confused with e which pertains to the base e.
i just want to minimize the equation using matlab.
y = -0.2662x6 + 48.19x5 - 3424.2x4 + 121708x3 - 2E+06x2 + 2E+07x - 6E+07
am i doing the right thing? i really need help sir.
I am assuming the size of your variable vector x is 1.
Try the following:
Create a 'fun' function
function F = fun(x)
F = -0.2662*x^6 + 48.19*x^5 - 3424.2*x^4 + 121708*x^3 - 2*exp(6)*x^2 ...
+ 2*exp(7)*x - 6*exp(7);
end
And then
x0 = 0;
[x, fval] = fminunc (@fun,x0)
in the command window.
Let me know if that's what you are looking for.
what do you mean that 'fun' doesnt seem to be the case?i think F should be a scalar in the end. -.-
function F = fun(x) takes a vector x and should return a scalar. But in your initial question you used x0 = [0, 0] implying the size of your variable x is 2 and when plugged in F it doesn't return a scalar.
x0 = 0; [x, fval] = fminunc (@fun,x0) ??? Error using ==> exp Not enough input arguments.
Error in ==> fun at 2 F = -0.2662*x^6 + 48.19*x^5 - 3424.2*x^4 + 121708*x^3 - 2*exp^6*x^2 + 2*exp^7*x - 6*exp^7;
Error in ==> fminunc at 227 f = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
(SEE TOP)
that's what happened :(((((
Please follow my previous comment clearly. its exp(6) and not exp^6 and exp(7) and not exp^7
oh i am sorry..the result is....
x0 = 0; [x, fval] = fminunc (@fun,x0) Warning: Gradient must be provided for trust-region algorithm; using line-search algorithm instead. > In fminunc at 347
Solver stopped prematurely.
fminunc stopped because it exceeded the function evaluation limit, options.MaxFunEvals = 100 (the default value).
x =
NaN
fval =
NaN

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by