How to use fminsearch for least square error minimization?

조회 수: 19 (최근 30일)
Muhammad Affan Arif
Muhammad Affan Arif 2021년 7월 25일
댓글: Muhammad Affan Arif 2021년 8월 24일
Hi everyone,
I am doing a Modal Parameter Estimation problem. I have measured values, and a function for numerical values. There is an error, which I need to minimize. But when I use fminsearch, it says that the dimensions on left hand side don't agree with that of right hand side. Becuase, fminsearch only gives 1x2, while the error (objective function) is 1x269.
I have used the following MATLAB commands:
e=@(uk) (abs(data_1(2561:2819,4))-abs((2i.*Hr.*uk(2).*uk(1).*uk(1))./(((uk(1).^2)-(ws.^2) + 2i.*uk(2).*uk(1).*ws))).^2
fminsearch(e,[413.4,0.0034])
Here, ws = 400:0.155:440
Any suggestions? Thank you for your time.
  댓글 수: 2
Rik
Rik 2021년 7월 25일
You need to design a function that returns a scalar. Then fminsearch will adjust the starting guesses to minimize that function.
Muhammad Affan Arif
Muhammad Affan Arif 2021년 7월 26일
@Rik So you mean, I need to design a function that minimizes the objective function at each data point?

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

채택된 답변

Rik
Rik 2021년 7월 26일
편집: Rik 2021년 7월 27일
I mean your objective function must only return 1 value, regardless of the shape of your data.
This is the standard ordinary least squares cost function. You need to provide a handle to your function, your beta will be determined by fminsearch, and you need to know the true value.
t=linspace(0,2*pi,100);
f=@(beta) sin(beta(1)*t+beta(2));
initial_guess=[1 1];
y_true=linspace(0,10,100);
OLS=@(f,beta,y_true) sum((f(beta)-y_true).^2,'all');
beta_fitted=fminsearch(@(beta) OLS(f,beta,y_true),initial_guess)
beta_fitted = 1×2
-0.0000 7.8540
Edit: sorry, I missed the squared part of the OLS.
  댓글 수: 1
Muhammad Affan Arif
Muhammad Affan Arif 2021년 8월 24일
Thank you, very much. It solved my issue.
I apologize for late acknowledgement.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by