Perform linear regression to obtain k

2 visualizzazioni (ultimi 30 giorni)
Deepa
Deepa il 5 Mar 2021
Risposto: Mathieu NOE il 5 Mar 2021
I am given the following data set and given an equation CA/CA0=e^-(kt). I am asked to perform a linear regression to find "k".
t=[0 500 1000 1500 2000 2500 3000];
CA=[0.1 0.0892 0.0776 0.0705 0.0603 0.0542 0.0471];
I am not sure how to use this equation exactly in the code or even if i need to find k. I am new to Matlab, it is a bit confusing
My code so far:
t=[0 500 1000 1500 2000 2500 3000];
CA=[0.1 0.0892 0.0776 0.0705 0.0603 0.0542 0.0471];
c1=polyfit(t,CA,1);
norm(CA-polyval(c1,t))

Risposte (1)

Mathieu NOE
Mathieu NOE il 5 Mar 2021
hello
here you are ; your "k" is my "b_sol" value :
a_sol = -0.1108
b_sol = -2.1657e-04
c_sol = -9.5151e-04
d_sol = 0.1001
code :
t=[0 500 1000 1500 2000 2500 3000];
CA=[0.1 0.0892 0.0776 0.0705 0.0603 0.0542 0.0471];
x = t;
y = CA;
% exponential fit method
% model : y = a.*(1-exp(b.*(x-c))) + d
f = @(a,b,c,d,x) a.*(1-exp(b.*(x-c))) + d;
obj_fun = @(params) norm(f(params(1), params(2), params(3), params(4),x)-y);
sol = fminsearch(obj_fun, [y(end)-y(1),0,0,y(1)]);
a_sol = sol(1)
b_sol = sol(2)
c_sol = sol(3)
d_sol = sol(4)
y_fit = f(a_sol, b_sol,c_sol ,d_sol, x);
figure
plot(x,y,'r',x,y_fit,'-.k');
legend('data','exp fit');

Community Treasure Hunt

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

Start Hunting!

Translated by