Defining parameters in a function

13 vues (au cours des 30 derniers jours)
Frank
Frank le 3 Juil 2011
  • Hi. I want to find the minimum of a function using parameters I've defined elsewhere. i.e.
a = 2; b = 3;
[x fval] = fminbnd('a*x^2+b',1,10)
How can this be done?

Réponses (1)

Paulo Silva
Paulo Silva le 3 Juil 2011
clf
a = 2; b = 3;
fun=@(x,a,b)a*x^2+b;
[x fval] = fminbnd(@(x) fun(x,a,b),1,10)
%lets see if the values are correct
ezplot('2*x^2+3')
hold on
plot(x,fval,'r*')
text(x,fval,'\leftarrow Minimum','FontSize',18')
Be aware that the interval [1 10] for x isn't a good choice like you can see in the figure, the value you get from fminbnd isn't the minimum of the function, change the interval to [-2 2] and you get the minimum of the function.
  1 commentaire
Frank
Frank le 6 Juil 2011
Thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by