![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/601455/image.png)
Problem in the data fitting with custom equation
4 views (last 30 days)
Show older comments
Hi
I am facing some issue in the data fitting with the equation. I wanted to fit to my data with eqauation
P(t) = 2Pr (1- exp(-(t/t0)^n) (also attached in the image). and Pr <= 40 and n <=2 are the upper limits.
I wanted to fit my function for n, Pr and t0.
t = 1.00E-07 2.00E-07 5.00E-07 1.00E-06 2.00E-06 5.00E-06 1.00E-05 2.00E-05 5.00E-05 1.00E-04 2.00E-04 5.00E-04 1.00E-03 2.00E-03 5.00E-03
P(t) = 2.3800 4.4800 6.4500 9.0200 13.2400 16.2600 18.9700 22.9900 26.3300 28.7800 30.3100 31.6500 31.9500 32.0500 32.3400
if possible let me know the code also.
0 Comments
Accepted Answer
Mathieu NOE
on 29 Apr 2021
hello
here you are :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/601455/image.png)
t = [1.00E-07 2.00E-07 5.00E-07 1.00E-06 2.00E-06 5.00E-06 1.00E-05 2.00E-05 5.00E-05 1.00E-04 2.00E-04 5.00E-04 1.00E-03 2.00E-03 5.00E-03];
P = [2.3800 4.4800 6.4500 9.0200 13.2400 16.2600 18.9700 22.9900 26.3300 28.7800 30.3100 31.6500 31.9500 32.0500 32.3400];
f = @(a,b,c,x) 2*a.*(1-exp(-(x./b).^c));
obj_fun = @(params) norm(f(params(1), params(2), params(3),t)-P);
sol = fminsearch(obj_fun, [P(end)/2,1,1]);
Pr = sol(1)
t0 = sol(2)
n = sol(3)
figure;
semilogx(t, P, '+', 'MarkerSize', 10, 'LineWidth', 2)
hold on
semilogx(t, f(Pr, t0,n, t), '-');grid on
xlabel('time t');
ylabel('P(t)');
% % gives :
% Pr = 15.9313
% t0 = 1.2114e-05
% n = 0.4339
2 Comments
Mathieu NOE
on 29 Apr 2021
hello
these are initial guesses , not limits;
fminsearch Multidimensional unconstrained nonlinear minimization (Nelder-Mead).
X = fminsearch(FUN,X0) starts at X0 and finds a local minimizer X of the
function FUN. FUN accepts input X and returns a scalar function value
F evaluated at X. X0 can be a scalar, vector or matrix.
More Answers (0)
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!