data fitting to equation
Show older comments
I have data for time(t) and pressure (p).
I want to fit these data to equation

I have done simple calcualtion and fittings in matlab. plese suggest how to fit data.
12 Comments
Bjorn Gustavsson
on 24 Feb 2021
Your model-function doesn't make sense as written. If that is what you want, you at least should know that that sum does not trivially converge since the exponential factor is independent of n and therefore can be factored out of the sum. This leaves you with:
That sum does not converge. You must first fix that.
Sudhir Rai
on 24 Feb 2021
Bjorn Gustavsson
on 24 Feb 2021
Check the variation of the sum for upper limits from 2 to 50, and then from 1000 to 1010. Then think about what this means.
Walter Roberson
on 24 Feb 2021
The sum of (-1)^n starting from n = 1 is:
- 0 if n is even
- -1 if n is odd
We still suspect that you did not write the equations properly, or else that C2 is somehow a function of n in a way that you did not show.
Sudhir Rai
on 25 Feb 2021
Bjorn Gustavsson
on 25 Feb 2021
No it's not.
See here for the first few partial sums for t = 1/C_2: [-e 0 -e 0 -e 0 -e 0]
What you've written is not any analytical solution to Fick's law. It is pretty clear you intended to write something else, and think you wrote something else.
Sudhir Rai
on 25 Feb 2021
syms c_1 c_2 d t real
syms n integer
assumeAlso(t >= 0 & n >= 0)
p(t) = c_1/d * (1+2*symsum((-1)^n * exp(-c_2*n^2*t/d^2), n, 1, inf))
Could you confirm that your parameters are c_1, c_2, and d ? If so then you can reduce by one parameter,
syms C_1 C_2
p2 = subs(p, {c_1, c_2}, {C_1*d, C_2*d^2})
subs(p2(t), {C_1, C_2}, {1/2, 3})
char(ans)
Walter Roberson
on 25 Feb 2021
It looks like
C_1 * (1+2*symsum((-1)^n * exp(-C_2*n^2*t), n, 1, inf))
is equal to
C_1 * JacobiTheta4(0, exp(-C_2*t))
Walter Roberson
on 26 Feb 2021
What scale of values are you expecting for c_1, c_2, d ? And what range of values are you expecting for time?
Unless you are dealing with quite small c_2/d and quite small t, then for floating point purposes the sum can be truncated at a quite small number of terms, such as 4 (or possibly fewer.) (Too few terms can be deceptive for small enough c_2/d )
Sudhir Rai
on 10 Jan 2023
Sudhir Rai
on 10 Jan 2023
Answers (1)
Just Manuel
on 24 Feb 2021
Refer to this answer from Star Strider:
You can fit any function using simple least squares regression. Just formulate your function (i guess you have already done that) in matlab, then make a cost function (least squares) and use fminsearch to optimize parameters c1 and c2
P = @(c, t) ... % your function
cost = @(c) sum((P(c,t) - p).^2);
% guess initial parameters
c_guess = [1 1];
% use fminsearch
c = fminsearch(cost, guess);
Cheers
Manuel
2 Comments
Sudhir Rai
on 10 Jan 2023
Walter Roberson
on 10 Jan 2023
Your code 2 1/2 years ago did not involve fmincon at all, so we cannot guess what your current code looks like.
Categories
Find more on Mathematics 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!