Filter löschen
Filter löschen

how to perform data fit like excel? and plot

4 Ansichten (letzte 30 Tage)
Anand Ra
Anand Ra am 16 Jun. 2021
Kommentiert: Anand Ra am 26 Jun. 2021
  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 Kommentare
Walter Roberson
Walter Roberson am 16 Jun. 2021
L2 = zeros(14);
that should probably be
L2 = zeros(14,1);
like the other variables.
Anand Ra
Anand Ra am 16 Jun. 2021
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Jun. 2021
Bearbeitet: Walter Roberson am 16 Jun. 2021
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 Kommentare
Anand Ra
Anand Ra am 25 Jun. 2021
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 am 26 Jun. 2021
Did I make any mistake like earlier with the code? Is there a way to get a good fit with an arbitrary initial guess?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by