필터 지우기
필터 지우기

What am I doing wrong!?

조회 수: 3 (최근 30일)
Will
Will 2012년 2월 7일
Hi,
I have the following code, it's part of a larger file that is trying (trying being the crucial word here) to perform optimisation using powell's method:
% testing
clear
a = zeros(1,1);
x0 = [0.2, 0.4, 0.6];
s1 = [0.4, 0.4, 1.6];
dx1 = a*x0;
x1 = x0 + dx1;
func = (x1(1)-x1(2))^2+2*(x1(2)-x1(3))^2+3*(x1(3)-1)^2;
[a] = feval(func, x1(1), x1(2), x1(3));
What I am trying to do is find the value of 'a' that minimises 'func'?
Thanks

채택된 답변

Kevin Holst
Kevin Holst 2012년 2월 7일
Ah I see the problem now. In your original post you had
s1 = [0.4, 0.4, 1.6];
but nothing using s1 in it. Now that I see what s1 is used for here's the solution:
x0 = [0.2, 0.4, 0.6];
s1 = [0.4, 0.4, 1.6];
x1 = @(n,a) x0(n) + s1(n)*a;
func = @(a)(x1(1,a)-x1(2,a))^2+2*(x1(2,a)-x1(3,a))^2+3*(x1(3,a)-1)^2;
a = fminsearch(func, 0);
a = 0.1364
  댓글 수: 1
Will
Will 2012년 2월 7일
Thank you so much! That's exactly what I wanted. Are you able to explain what having the (n) does? I would understand if it was in a 'for' loop.

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

추가 답변 (3개)

Sean de Wolski
Sean de Wolski 2012년 2월 7일
Syntax issues:
a = zeros(1,1);
x0 = [0.2, 0.4, 0.6];
s1 = [0.4, 0.4, 1.6];
dx1 = a*x0;
x1 = x0 + dx1;
func = @(x)(x(1)-x(2))^2+2*(x(2)-x(3))^2+3*(x(3)-1)^2;
a = fminsearch(func, x1(1:3));
  댓글 수: 2
Will
Will 2012년 2월 7일
Thank you very much!
Is 'feval' still used?
What does @(x) do to the 'func' line?
Will
Will 2012년 2월 7일
Also, that code works for finding the values of x that minimise func. I am trying to find the value of 'a' which is a scaler multiplier.

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


Matt Kindig
Matt Kindig 2012년 2월 7일
This is not the approach you want to follow. First, you are calling feval, which will not minimize anything, only evaluate a specified. Instead, you will need one of the solvers in the Optimization Toolbox that is designed to minimize a function, such as fminunc. You will pass a function handle into fminunc that defines your function with the variable you wish to change (a) as the input parameter. Something like this should work:
x0 = [0.2, 0.4, 0.6];
s1 = [0.4, 0.4, 1.6]; %note that you don't use this anywhere
func = @(a) ((a+1)*(x0(1)-x0(2))).^2 + 2*((a+1)*(x0(2)-x0(3))).^2 + 3*( (a+1)*(x0(3)-1)).^2;
a = fminunc(func, 0);
  댓글 수: 1
Will
Will 2012년 2월 7일
Thanks for the replies but neither of those suggestions give me the answer I am trying to obtain, probably because I haven't explained it. This is the maths of what I want to achieve.
Original function f1=(x1-x2)^2+2*(x2-x3)^2+3*(x3-1)^2 X1=X0+a1*S1 X1=[0.2 0.4 0.6]+a1*[0.4 0.4 0.6]=[0.2+0.4a1 0.4+0.4a1 0.6+1.6a1]
Substituting the new values of x1,x2,x3 into the original function yields:
fa1=-0.2^2+2*(-0.2-1.2a1)^2+3*(-0.4+1.6a1)^2
Minimisation of this function should return a value of a1=0.1364
Thanks

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


Kevin Holst
Kevin Holst 2012년 2월 7일
I'd suggest using the following code, it's not pretty, but it gets the job done without any toolboxes.
x0 = [0.2, 0.4, 0.6];
x1 = @(n,a) (1+a)*x0(n);
func = @(a)(x1(1,a)-x1(2,a))^2+2*(x1(2,a)-x1(3,a))^2+3*(x1(3,a)-1)^2;
a = fminsearch(func, 0);
  댓글 수: 1
Will
Will 2012년 2월 7일
Thanks for the replies but neither of those suggestions give me the answer I am trying to obtain, probably because I haven't explained it. This is the maths of what I want to achieve.
Original function f1=(x1-x2)^2+2*(x2-x3)^2+3*(x3-1)^2 X1=X0+a1*S1 X1=[0.2 0.4 0.6]+a1*[0.4 0.4 0.6]=[0.2+0.4a1 0.4+0.4a1 0.6+1.6a1]
Substituting the new values of x1,x2,x3 into the original function yields:
fa1=-0.2^2+2*(-0.2-1.2a1)^2+3*(-0.4+1.6a1)^2
Minimisation of this function should return a value of a1=0.1364
Thanks

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by