How to replace a value in function

조회 수: 3 (최근 30일)
Elanorin Weaving
Elanorin Weaving 2015년 6월 20일
댓글: Azzi Abdelmalek 2015년 6월 21일
Hello, I automate the following function
f=@(x)0;
for i=1:3
h=@(x)x(i)^2
f=@(x) f(x) +h(x) % f= x(1)^2+x(2)^2+x(3)^2
end
The ISSUE IS:I want to replace x(3) =3 , so that the function would instead represent f=x(1)^2+x(2)^2+9. Is it possible?
Background information: My main goal is to to automatically create an abstract function (not necessarily the one mentioned above), replace it with known values of x and minimize it.
Any help whatsoever is hugely appreciated!
Thank you, Ela

답변 (2개)

Image Analyst
Image Analyst 2015년 6월 20일
Abandon all that complicated nonsense, and simply do this:
x = 1 : 3;
f = sum(x.^2)
  댓글 수: 1
Elanorin Weaving
Elanorin Weaving 2015년 6월 20일
The actual function i want to minimize is something like:
((((x(3)-x(1))^2 + (x(4)-x(2))^2 -25)/25)^2 +(((x(5)-x(1))^2 + (x(6)-x(2))^2 -9)/9)^2) +2*x(2)
I am able to generate the function. Although would appreciate a more optimal way.
The ISSUE is, I want to substitue x(4)=5, x(3) = 4 so that the function would look like ((((4-x(1))^2 + (5-x(2))^2 -25)/25)^2 +(((x(5)-x(1))^2 + (x(6)-x(2))^2 -9)/9)^2) +2*x(2)
and I can minimize the above.

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 21일
f=@(x) x^2
som=0
for k=1:3
som=som+f(k)
end
  댓글 수: 2
Elanorin Weaving
Elanorin Weaving 2015년 6월 21일
Thanks for the response! I don't have problems creating the function, but rather replacing a variable in function.
For eg: I am able to create f(x) = x(1)^2+x(2)^2. Is there anyway to make the function f(x) = x(1)^2 + 4, and minimize it.
Thanks again!
Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 21일
f=@(x) sum(x.^2)
f(1:3)

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by