Fitting 8 equations simultaneously with three parameters

조회 수: 9 (최근 30일)
Vahid Askarpour
Vahid Askarpour 2025년 9월 19일
댓글: Vahid Askarpour 2025년 9월 19일
I have 8 equations each with three parameters (A, B, and C) to be fitted. For example,
Eq. 1: 2A+3B+5C-1=0
Eq. 2: -5A+2B-10C+12=0 and so on
I would like to fit the three parameters so that all the 8 equations almost hold simultaneously. Having looked at other posts, it seems that lsqnonlin may be the right choice. I am not looking for the actual solution but I would appreciate a yes or no to the use of lsqnonlin for this problem.
Thanks,
Vahid

채택된 답변

Torsten
Torsten 2025년 9월 19일
편집: Torsten 2025년 9월 19일
If all 8 equations are linear in the fitting parameters like in your example from above, "lsqlin" instead of "lsqnonlin" would be the code to use. If this is not the case, yes: use "lsqnonlin".
  댓글 수: 2
Vahid Askarpour
Vahid Askarpour 2025년 9월 19일
Thanks Torsten for the tip. They are all linear and I will be using lsqlin.
Torsten
Torsten 2025년 9월 19일
편집: Torsten 2025년 9월 19일
If there are no constraints on the solution parameters, @Star Strider 's suggestion is also possible to use:
% Eq. 1: 2A+3B+5C-1=0
% Eq. 2: -5A+2B-10C+12=0
% Eq. 3: 9A-B+2C+5=0
% Eq. 4: 4A-7B+6C-13=0
ABC = lsqlin([2 3 5; -5 2 -10; 9 -1 2; 4 -7 6],[1; -12; -5; 13])
ABC = 3×1
-1.0118 -1.2492 1.4242
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ABC = [2 3 5; -5 2 -10; 9 -1 2; 4 -7 6] \ [1; -12; -5; 13]
ABC = 3×1
-1.0118 -1.2492 1.4242
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

추가 답변 (1개)

Star Strider
Star Strider 2025년 9월 19일
You might be able to use the mldivide, \ function --
Example --
% Eq. 1: 2A+3B+5C-1=0
% Eq. 2: -5A+2B-10C+12=0
% Eq. 3: 9A-B+2C+5=0 % <- ADDED
ABC = [2 3 5; -5 2 -10; 9 -1 2] \ [1; -12; -5]
ABC = 3×1
-1.0284 -1.3785 1.4385
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fprintf('\nA = %9.5f\nB = %9.5f\nC = %9.5f\n',ABC)
A = -1.02839 B = -1.37855 C = 1.43849
.
  댓글 수: 1
Vahid Askarpour
Vahid Askarpour 2025년 9월 19일
Thank you Star Strider and Torsten for your comments. I used both your suggestions and I get exactly the same answer.
Cheers,
Vahid

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

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by