# Thread: Mathematica FindFit - proper format question

1. ## Mathematica FindFit - proper format question

Hi everyone,

Thanks in advance for your help. I'm trying to fit a system of coupled ODEs to some experimental data; currently executing my code results in the function returning itself. The documentation claims this means that there is no syntax errors but Mathematica doesn't know what to do with my input (i.e. I'm doing something wrong, but it isn't blatant to Mathematica).

I'm new to Mathematica so without errors to guide me (and without finding anything helpful on the web), I'm at a loss.

The system of ODEs is somewhat complicated, so here is a simple example that does the same thing:

Code:
system={y1'[t]==A,y2'[t]==B};
pars={A,B};
vars={y1[t],y2[t]};
expvalues={{0,0,0},{1,5,10},{2,10,Null}};
FindFit[expvalues,system,pars,vars]
Obviously this is a trivial example; I'm trying to fit y1=A*y1 and y2=B*y2 to some sample data where A=5 and B=10.

I put "Null" in to the second set of experimental values because in my actual system I have some blanks in my experimental values (e.g. at time=10 I have input for 3 of the 5 variables, and at time=20 I have input for a different 3 of the 5 variables). I'm not sure if that is the correct way to put an "empty" value in to the list (and it does seem that you have to have something for each variable at each time point you're giving data), but if you replace the Null with a "20" in the code you get the same error, so that isn't the only issue with what I've done.

Thanks again!

2. I don't think you can do multiple equation fits with FindFit. Can you not call FindFit for each equation? The general format is:

FindFit[data, expres, param,varab]

The "expres" is a single expression and I don't think it can have derivative specifiers in it like you have above with the primes. It can have multiple variables and multiple parameters but still it's for a single fit function.

3. Thanks for the response - I suspect you may be right, but I can't just fit each one separately since in my actual system the equations are interrelated. If anyone has ideas as to how to go about fitting a system of equations, I'm all ears! This was the best approach I've been able to find so far....

4. How many equations do you have?

After re-reading your post, I think you are trying to fit the "solutions" of the DE system to your experimental data. That's not the same as using FindFit to fit an equation to a set of data points.

Say I have a set of first-order equations in n variables and a set of experimental data that should correspond to the solution of this system. If they are simple enough for the function DSolve to solve explicitly, then I'd solve it and then determine the best values for the arbitrary constants that are created during the solution that fits the experimental data.

More likely they are not simple so you have to numerically integrate the system via NDSolve. But in this case you must supply initial conditions for each variable before you run NDSolve. That's more difficult. There is no Mathematica program that would allow you to find the best initial conditions to use that would best fit the experimental data but you can determine that to some level of success programatically: try any initial conditions, obtain the solutions by NDSolve, inspect them for fit, change the initial conditions and keep doing this to manually determine the best initial conditions to use that best fits the data.

5. Yes, the data is being fit to the solution of the equations (there are 5). I know the initial variable conditions, so that doesn't add any additional complexity. What I don't know are the parameter values - I have done manual changes to the parameters to determine a reasonable fit (using another language), and I'm hoping to do an automated search to optimize them further.

So you're suggesting I run NDSolve with my system of equations (DSolve won't do it, as you suggest), and input the results into FindFit?

6. Ok. That's a start. When you say parameters, you mean in the differential equations themselves? Are they first order and linear (and autonomous)? Do they look like:

$x_1'=a_1 x_1+a_2 x_2+a_3 x_3+a_4 x_4+a_5 x_5,\quad x_1(0)=k_1$

$x_2'=b_1 x_1+b_2 x_2+b_3 x_3+b_4 x_4+b_5 x_5,\quad x_2(0)=k_2$

$x_3'=c_1 x_1+c_2 x_2+c_3 x_3+c_4 x_4+c_5 x_5,\quad x_3(0)=k_3$

$x_4'=d_1 x_1+d_2 x_2+d_3 x_3+d_4 x_4+d_5 x_5,\quad x_4(0)=k_4$

$x_5'=f_1 x_1+f_2 x_2+f_3 x_3+f_4 x_4+f_5 x_5,\quad x_5(0)=k_5$

7. They're first order but nonlinear and not autonomous, as in:
$x_1'=a_1x-(a_2 x_1/(x_1+a_3))(x_2+x_3+x_4+x_5)$

I forget the exact form of the others (they're not with me at home over the weekend ), but they're similar in general form; for instance the most complicated term in all of them is $x_1/(x_1+a_3)$. The initial conditions are all integer values.

By parameters I mean non-variables, so the "a" terms as opposed to the "x" terms above.

Thanks again for your continuing help!

8. Ok. I've thought about it and now feel this is not at all easy. Consider just a 2x2 system with four parameters:

$x'=ax+by$

$y'=cx+fy$

That's already a very complicated system when you consider all possibilities of the eigenvalues and further, the cases are qualitatively different. It's an interesting concept and I would approach it by first studying the 2x2 system to see if it's even possible to determine effectively, then maybe scale-up to a 3x3 system. But I wouldn't start with your 5x5 system.

9. Ok, thanks very much!