computing function with a variable

1 vue (au cours des 30 derniers jours)
Amal Fennich
Amal Fennich le 26 Fév 2021
Hi , I want to compute this in Matlab :
x0=[1;2;3;4];
d0=[1118,-124,-502,-1090];
F=(x1-10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4;
f(alpha)=f(x0+alpha*d0);
how can I do that ??? help please
  5 commentaires
Rik
Rik le 26 Fév 2021
What is it exactly what you mean? What is your intention? Your code is not valid, nor is it clear to me what it should be doing. Do F and f have a relation?
Simon Allosserie
Simon Allosserie le 26 Fév 2021
Modifié(e) : Simon Allosserie le 26 Fév 2021
Pleas read this on functions, should be able to help you out. https://nl.mathworks.com/help/matlab/ref/function.html

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 26 Fév 2021
%{
x0=[1;2;3;4];
d0=[1118,-124,-502,-1090];
F=(x1-10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4;
%}
F is not a function. Assuming x1, x2, x3, and x4 exist before this line of code executes it will likely be a numeric array (though you're probably going to want to vectorize your code so it can process an array of data all at once.) If x1, x2, x3, and x4 did not exist when this line was called, it would throw an error.
And no, the variables x1, x2, x3, and x4 are not related in any way, shape, or form to the elements of the variable x0. In particular x3 is not 3 just because x0(3) is 3.
If you wanted to make F a function I would probably make F a function of one variable where that variable is a vector, something like:
F = @(x) x(1)+x(2).^2;
F([3 5]) % 3 + 5.^2 = 28
ans = 28
Looking at your last line of code:
%{
f(alpha)=f(x0+alpha*d0);
%}
there are several problems. alpha is not defined in your code so MATLAB will try to call the alpha function when it sees that identifier. You also have not defined any variable f (f and F are not the same thing) in your code. Finally, even if alpha were defined I'm guessing you'd want this to work for values of alpha that are not positive integer values. There's no such thing as element 0.5 of an array in MATLAB, for example. You could do something like this using the function F I defined.
Fvalue = F([3, 5] + 0.1*[1, 2]) % x0 is [3, 5], alpha is 0.1, d0 is [1, 2]
Fvalue = 30.1400

Catégories

En savoir plus sur Get Started with MATLAB 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