data fitting to equation

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

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.
thanks for your comment.
If I terminate it to some large value of n, may be n=1000. In that case, Is it possible to fit my P & t data?
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.
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.
This is analytical solution of diffusion equation (fick's law). where 't' is time and c1, c2 includes diffusivity term along with differential equation constants.
I combined all constant to visualize it as proper mathematical equation.
while solvimg this type of equation I usually check the variation in 'P' by increasing value of 'n' (if i know constants).
But here I want to find out constants by fitting my experimental results for t & p to this equation for some higher value of n. I have checked that for n=10000, my value may be constant for p if i put constants from other results.
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.
Thank you for pointing out, i have missed n^2 inside exponential term.
And this is analytical solution of diffusion equation with BC for my case. I have taken d out of constant term which is thickness for diffusion
The corrected equation:
p(t)=(c_1/d)*(1+2*sum(1,inf) [(-1)^n*exp(-c_2*n^2*t/d^2)
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))
p(t) = 
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})
p2(t) = 
subs(p2(t), {C_1, C_2}, {1/2, 3})
ans = 
char(ans)
ans = 'symsum((-1)^n*exp(-3*n^2*t), n, 1, Inf) + 1/2'
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 )
d is constant that is length of diffusion . range of C_1, c_2 :
c_1 =1e-13 to 1e-17 and c_2=1e-9 to 1e-11 approximatly.
d is ~ 1e-4
t is time which could go upto 2000 s

Sign in to comment.

Answers (1)

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

I am using for loop for my function. But I am facing problem in using loop output with fminsearch.
and it is not optimizing my parameters.
Could you help me with it?
Your code 2 1/2 years ago did not involve fmincon at all, so we cannot guess what your current code looks like.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 20 Aug 2020

Commented:

on 10 Jan 2023

Community Treasure Hunt

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

Start Hunting!