computing function with a variable
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2021-2-26
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
2021-2-26
编辑:Simon Allosserie
2021-2-26
Pleas read this on functions, should be able to help you out. https://nl.mathworks.com/help/matlab/ref/function.html
回答(1 个)
Steven Lord
2021-2-26
%{
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
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!