how to perform data fit like excel? and plot

조회 수: 1 (최근 30일)
Anand Ra
Anand Ra 2021년 6월 16일
댓글: Anand Ra 2021년 6월 26일
  1. I have observed array of data ( y_obs) and predicted data (y_pred)
  2. Predicted data is obtained from an equation
  3. How do I fit the observed data to the predicted data by minimizing the coefficient "d" in the equation? ( This is possible in excel, but I could not find a suitable method in matlab
Below is my code for steps 1 and 2:
% observed data
y_obs = [0.3 0.2 0.28 0.318 0.421 0.492 0.572 0.55 0.63 0.61 0.73 0.8 0.81 0.84 0.93 0.91]'; % If y_obs should equal to predicted, I can have more data. J us fo rthe code, I am providing limited observed data
t1 = [300:300:21600]';
a=0.0011;
gama = 0.01005;
d=0.000000000302;
n=1;
t=300;
L2 = zeros(14,1);
L3 = zeros(14,1);
L4 = zeros(14,1);
At = zeros(14,1);
t = 300;
n =1;
L1 = ((8*gama)/((pi*(1-exp(-2*gama*a)))));
format shortE
for t= 300:300:21600
for n=1:1:14
L2(n) = exp((((2*n + 1)^2)*-d*pi*pi*t)/(4*a*a));
L3(n) = (((-1)^n)*2*gama)+(((2*n+1)*pi)*exp(-2*gama*a))/(2*a);
L4(n)= ((2*n)+1)*((4*gama*gama)+((((2*n)+1)*pi)/(2*a))^2);
L5(n) = ((L2(n).*L3(n))/L4(n));
end
S(t/300) = sum(L5);
y_pred(t/300) = 1 -L1*S(t/300); % predicted data
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 6월 16일
L2 = zeros(14);
that should probably be
L2 = zeros(14,1);
like the other variables.
Anand Ra
Anand Ra 2021년 6월 16일
Thanks for the response, I can update it.
Can you please guide me on how to perform the data fitting in the fashion I described in bullet point 3?

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 16일
편집: Walter Roberson 2021년 6월 16일
format shortE
% observed data
y_obs = [0.3 0.2 0.28 0.318 0.421 0.492 0.572 0.55 0.63 0.61 0.73 0.8 0.81 0.84 0.93 0.91]'; % If y_obs should equal to predicted, I can have more data. J us fo rthe code, I am providing limited observed data
t1 = [300:300:21600]';
T1 = t1(1:length(y_obs)).';
a=0.0011;
gama = 0.01005;
d0 = 0.000000000302;
syms d
n=1;
t=300;
L2 = sym(zeros(14,1));
L3 = sym(zeros(14,1));
L4 = sym(zeros(14,1));
At = sym(zeros(14,1));
t = 300;
n =1;
L1 = ((8*gama)/((pi*(1-exp(-2*gama*a)))));
y_pred = sym(zeros(length(T1),1));
for t = T1
for n=1:1:14
L2(n) = exp((((2*n + 1)^2)*-d*pi*pi*t)/(4*a*a));
L3(n) = (((-1)^n)*2*gama)+(((2*n+1)*pi)*exp(-2*gama*a))/(2*a);
L4(n)= ((2*n)+1)*((4*gama*gama)+((((2*n)+1)*pi)/(2*a))^2);
L5(n) = ((L2(n).*L3(n))/L4(n));
end
S(t/300) = sum(L5);
y_pred(t/300) = 1 -L1*S(t/300); % predicted data
end
sse = expand(sum((y_pred - y_obs(:)).^2));
f = matlabFunction(sse)
ans = 
opt1 = fmincon(f, d0)
opt2 = fminsearch(f, d0)
  댓글 수: 35
Anand Ra
Anand Ra 2021년 6월 25일
However, I had to keep attempting the inital value to get the right number that would produce a fit. The optimized coefficient is same as my initial assumption.
When I tried with different datab set for y_obs, I am unable to find that perfect inital guess that would produce me a good fit.
Not sure what is going wrong.
Anand Ra
Anand Ra 2021년 6월 26일
Did I make any mistake like earlier with the code? Is there a way to get a good fit with an arbitrary initial guess?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Oceanography and Hydrology에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by