# how to use lsqnonlin to solve conditional equation

조회 수: 1(최근 30일)
cheng li 2021년 9월 5일
댓글: cheng li 2021년 9월 8일
i would like to input the following equation in matlab and use lsqnonlin to find the C0, Cinfi1, k1, t2start, Cinfi2 and k2 that can fit an obriginal data?
Are there anyone knowning how to do that?

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

### 채택된 답변

Fabio Freschi 2021년 9월 7일
편집: Fabio Freschi 2021년 9월 7일
The following code should be self explainatory. In the opposite case, simply ask
clear all, close all
% some dummy params values
C0 = 2;
Cinf1 = 10;
k1 = 2;
t2start = 3;
Cinf2 = 8;
k2 = 3;
% t vector
tData = linspace(0,10,50);
% data + noise
rng(0); % for reproducibility
yData = C0+Cinf1*(1-exp(-k1*tData))+(tData >= t2start).*(Cinf2*(1-exp(-k2*(tData-t2start))))+0.3*randn(size(tData));
% anonymous function for fitting (function-data) that must be minimized
% using least squares
% x(1) = C0
% x(2) = Cinf1
% x(3) = k1
% x(4) = t2start
% x(5) = Cinf2
% x(6) = k2
C = @(x)x(1)+x(2)*(1-exp(-x(3)*tData))+(tData >= x(4)).*(x(5)*(1-exp(-x(6)*(tData-x(4)))))-yData;
% initial values (experience may help here)
x0 = [1 1 1 1 1 1];
% fitting
x = lsqnonlin(C,x0);
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% plot
figure, hold on
plot(tData,yData,'o');
% reconstruction of the best fit from the anonymous function
plot(tData,C(x)+yData);
legend('data','best fit');
##### 댓글 수: 1표시숨기기 없음
cheng li 2021년 9월 8일
thank you so much

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

### Community Treasure Hunt

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

Start Hunting!

Translated by