computing function with a variable
조회 수: 2 (최근 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에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!