Message Boards Message Boards

0
|
2059 Views
|
42 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How do I simulate with three dependent differential equations in Mathematica

Posted 3 months ago

I am a relatively new user to Mathematica. Could someone tell me the best and most efficient way to code in Mathematica these three differential equations, shown in the image, for a three gene negative feedback loop? I have included an image below of how I was doing this previously with brute force in Excel using small time increments of 0.000025 min. Obviously, Mathematica is a much better way to do this calculation. You can see that each differential equation includes the gene product calculated by one of the other two differential equations so all 3 equations are interlinked. I would like to be able to simulate various three-gene negative feedback systems by changing the parameters on the left-hand side of the image below.

Thank you in advance for any help that you can provide me on coding in these three differential equations into Mathematica and informing me on the best way to do simulations in Mathematica.

Three gene negative feedback loop

POSTED BY: Scott Klakamp
42 Replies

What does [x], [y], [z] mean? Are those beta,m,K constants or functions?

POSTED BY: Gianluca Gorni
Posted 3 months ago

x,y, and z are the three different proteins that are made by the three-gene negative feedback loop and described by the three differential equations. Thanks for any help you can give me.

POSTED BY: Scott Klakamp
Posted 3 months ago

EDIT: I got Excel to show me the contents of all the cells and reconstructed your equations from that

POSTED BY: Bill Nelson
Posted 3 months ago

Bill,

I have attached a truncated version of my Excel file (the complete original file is 129 MB with 1e6 rows in it). You can see the formulas in the sheet that I used to simulate the concentrations of proteins x, y, and z from the differential equations for x, y, and z formation with respect to time. x, y, and z are the proteins made in the negative feedback system from the three genes, x, y, and z. All the parameters are defined in the Excel sheet too that are used in the three differential equations. Thank you for any help you can render to me.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

I have done some more research on how to simulate with three coupled differential equations, but the code i have written in the attached Mathematica notebook is still not working. I am open to any advice to successfully simulate these three differential equations in Mathematica.

Attachments:
POSTED BY: Scott Klakamp
Posted 3 months ago

You are missing a semicolon on your first line between B2=1 and B3=1

If I fix that and then

sol=NDSolve[...yoursystem...];
Plot[Evaluate[{x[t],y[t],z[t]}/.sol],{t,0,100}]

Then I see your three plots. But the plots do not look very close to the plots you show from Excel. I am guessing that this may be because of the values for some of your coefficients when you tried to translate from discrete steps in Excel to derivatives in Mathematica. Perhaps you could estimate the derivatives by looking at the change in values from 0.0 to 0.000025 and the slope of those changes might be close enough to the derivatives that you need to give NDSolve

POSTED BY: Bill Nelson
Posted 3 months ago

Bill,

Thank you immensely for the help on this! Much appreciated. I did have a few of the parameters not set correctly like K2=5e7 not 1e7 and the starting condition for x[0] should have been 1e-6 not 0. However, the bad news is that even after setting those parameters and initial conditions correctly and setting the range from 0 to 0.000025 the curves do not match the Excel curves at all as you pointed out.

I think your suggestion is a good one if I understand it correctly. Do you mean NDSolve in Mathematica can be given a time increment like 0.000025 to do the numerical solution with or are you suggesting something else? At this point, I am not sure I am capable of doing either of your two potential solutions because I am not an expert in Mathematica.

Could you provide a little more detail on any ideas you have of overcoming the differences seen in the curves between Mathematica and Excel? It is hard for me to believe Excel would be better than Mathematica at this calculation, and it is more likely my incompetence in Mathematica may be causing the differences we are seeing in the curves. I am pretty confident that Excel (while taking forever with one million rows to do the calculation) is producing the correct curves.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Hi Scott,

I've also looked at this. When I use your equations and parameter values from your spreadsheet graphic, I see this: enter image description here What is most puzzling is that your graph appears to show that two of the protein concentrations approach the same steady state value. It is difficult to see a justification for that in the equations. Is it possible your spreadsheet does not correctly implement the differential equations shown?

Beyond that, I suggest that if Mathematica results (using adaptive time steps) differ from those in Excel using a fixed time step, then Mathematica is more trust worthy.

Attachments:
POSTED BY: David Keith
Posted 3 months ago

David,

Thank you for all the help. I will take a look at the notebook you attached. Your analysis is much appreciated. To answer your question about two of the proteins reaching the same steady-state--that is correct because two of the proteins (x and z) have all the same parameters so upon reaching equilibrium (steady-state in essence) their protein concentrations should be the same. For some reason, protein x and z are not reaching the same steady-state level in Mathematica. In regards to your second question, I did find an error in my Excel sheet (I attached a truncated version yesterday, which has the mistake in it). However, the mistake did not change the oscillatory nature of the curves and the correct curves are almost identical those you saw in my post 2 days ago, so I am still perplexed why I am not seeing the same behavior in Mathematica. The curves should oscillate and two of the proteins should reach the same steady state. I have attached a corrected truncated version of my Excel sheet in case you care to look at it. I can find no mistake in the way I did the analysis in Excel other than it is not eloquent and extremely slow due to 1 million rolls in Excel. If I understand what you did in your notebook, I believe you just told Mathematica how large of a time increment to take when solving the coupled differential equations; is that correct? If that is correct, it is even more puzzling to me because the increments appear very small, which is similar to what I used for time (0.000025 time increments) in Excel.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Hi Scott,

While x and z do have the same lifetimes, the coupling of equations breaks the symmetry. x'[t] depends on z[t], but z'[t] depends an y[t]. The equations with the parameters substituted look like this:

enter image description here If you set the derivatives equal to zero, and solve for the three concentrations, they are not the same. (That is what DFixedPoint does. Its new in 14.1.) Maybe I have made an error in the equations or parameters.

Regarding time stepping, I did not specify time step parameters to Mathematica. It has very clever algorithms for doing that.

POSTED BY: David Keith
Posted 3 months ago

Do you have the original differential equations and parameters that you used to develop your discrete model? That might make it easier to produce the differential equations that MMA can accept, solve and plot. Or did you develop the discrete model from scratch?

Both Excel and MMA can be used to approximate things like this. The work is trying to translate the information into a form that each of them can use.

POSTED BY: Bill Nelson
Posted 3 months ago

David,

Thank you for the input. It makes sense what you are saying. I went ahead and made all the parameters the same for each type of parameter for each of the three proteins. Under those conditions, all the proteins should reach the same steady-state level. You will see in the attached notebook they do not, so I am still puzzled by what is going on in Mathematica. When I simulated in Excel, all three proteins reached the same-steady level, which should be the case since all proteins are being made and broken down at the same rate. I wanted to do this comparison to try to figure out why there is disagreement between Excel and Mathematica.

Any further feedback would be appreciated.

Scott

Attachments:
POSTED BY: Scott Klakamp
Posted 3 months ago

Bill,

The equations in my notebook are the differential equations that I took from a textbook, Molecular Biology of the Cell, a classic in the field. I choose all parameter values myself (because the textbook does not give any particular values in the figures they show) by trial and error to generate an oscillatory behavior. It is well known that this negative feedback gene system described by the three differential equations should give oscillations in protein concentrations until steady-state is reached.

This seems like too easy of an answer to your question, so I may have missed what you are really asking me. If so, please get back to me.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

David,

Due to my inexperience with Mathematica, the last notebook I sent you did not evaluate correctly. In the newly attached notebook, i do see all proteins overlapping with one another, so that is pretty convincing evidence to me that Mathematica is working correctly. i wish i was a better mathematician because I still do not understand why Excel is not giving the correct curves. The only think I can think of is that 0.000025 is still not a small enough time increment to allow Excel to simulate the differential equations accurately.

In any event, I think I will believe the Mathematica curves more than the curves in Excel. Thank you again!

Scott

Attachments:
POSTED BY: Scott Klakamp
Posted 3 months ago

When I execute the notebook you attached I get this: enter image description here

I think you had some previously defined definitions still in the kernel. That can be deceptive. If you quit the kernel and then evaluate the notebook it will be done with a fresh kernel and you should see the same results.

Any chance you could scan the page with the equations from the text and attach to a post?

POSTED BY: David Keith
Posted 3 months ago

Oops -- you were typing this while I was responding above. ;-)

POSTED BY: David Keith
Posted 3 months ago

David,

yes, I screwed up the evaluation of that notebook--our messages crossed on the wire. It will not be the first and last time I do that I am sure! Mathematica is a real insider's software in my opinion.

I have attached the pages you requested, see Fig. 8-80. You can forget the hill coefficient exponents in the text. Hill exponents are for systems with positive or negative cooperativity (where binding of one ligand either increases the affinity or decreases the affinity of the next ligand binding event). This current system is complicated enough without throwing Hill exponents into the mix. That is easy to do later once the foundation is laid, which I think we are finally there now from the help of users on in this community like you. I am very grateful for the help on this.

Scott

Attachments:
POSTED BY: Scott Klakamp
Posted 3 months ago

I've tried and can't get my hands on a copy of your textbook at the moment.

If you could copy the differential equations from the text into a message here and include the values of the parameters that you are using at the moment then I will try to translate that system into Mathematica and plot the solutions and see how close my plots are to your posted Excel plots. I might be able to generate your entire Excel sheet from the example you gave, but I'm not sure I can do that correctly.

Hopefully we are going to get you what you need. Thanks for your patience and assistance with this.

POSTED BY: Bill Nelson
Posted 3 months ago

Bill,

I did misunderstand what you meant then. You were asking about the equations I used in Excel. Sorry that I totally missed that. I have attached the section below from the textbook dealing with these oscillatory, negative feedback systems. I did make some comments in my response to another user, David Keith, about that section, see Figure 8-80.

It will be interesting to see what the small time increment, step-wise Excel equations return in Mathematica. In Excel it takes about one million rows to get 30 min. of data with 0.000025 time increments.

Thank you for the curiosity and taking a look at those equations

Scott

Attachments:
POSTED BY: Scott Klakamp
Posted 3 months ago

David,

Could you also show me with one of my parameters (pick the one you want) how to add a slider bar with the Manipulate command? I believe you used pretty fancy coding of my equations, and I tried for about 90 min. to make the Manipulate function work and could not. I was able to get the 12 parameter sliders added, but somehow they are not linked to the calculation and do nothing. When I trained with the tutorials in Mathematica, the Manipulate function was always presented as Manipulate[Plot[Evaluate.....] and your notebook is written pretty differently from that example so I cannot figure out how to get Manipulate to work. If you give me one example, I can add the other 11 parameters since i want to be able to adjust the 12 parameters that go into this calculation to see what effect each has on the oscillation.

Sorry for the bother again!

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Hi Scott, I’ll have a look at this tomorrow.

David

POSTED BY: David Keith
Posted 3 months ago

Thank you for the text. That was invaluable.

In textbook Figure 8-83

d[X]/dt=betax mx 1/(1+(Ky[Y])^hy-[X]/taux
d[Y]/dt=betay my 1/(1+(Kx[X])^hx-[Y]/tauy

Mathematica

x'[t]==betax mx / (1+ky[y[t]]^hy)-x[t]/taux
y'[t]==betay my / (1+kx[x[t]]^hy)-y[t]/tauy

Excel Sheet image of 3 equations in upper right

dx/dt=betax mx/(1+Kz[z])-[x]/taux
dy/dt=betay my/(1+Kx[x])-[y]/tauy
dz/dt=betaz mz/(1+Ky[y])-[z]/tauz

Mathematica

x'[t]==betax mx/(1+kz[z[t]])-x[t]/taux
y'[t]==betay my/(1+kx[x[t]])-y[t]/tauy
z'[t]==betaz mz/(1+ky[y[t]])-z[t]/tauz

I am guessing in your Excel sheet that you chose

betax=1
betay=1
betaz=1
mx=1
my=1
mz=1
taux=2
tauy=1.5
tauz=2

Now I need to understand your K functions. Is

Kx[z]=10^8*z?

That doesn't seem right. Likewise for Ky and Kz

It looks like he is describing a variety of different situations that can arise and I'm not certain yet just what of those you are trying to do in your model

If I can understand those last three and I haven't made any other mistakes then I think I'm close to pasting this into Mathematica and getting a plot.

POSTED BY: Bill Nelson
Posted 3 months ago

Bill,

Thank you for all the work! I chose Kx=1e8, Ky=5e7, and Kz=1e8, which could be realistic affinities (1/K) of 10 nanomolar and 20 nanomolar. Figure 8-83 is for the two-gene protein negative feedback loop. i chose the three-gene negative feedback system because the more genes the more the system oscillates. A five gene loop would have been even better, but I did not want to get carried away. You can leave the Hill coefficients (hx or y) out, if you would like, because a system with no cooperativity has hx or y=1. This system is complicated enough without more complexity. I choose all those 12 parameters arbitrarily though trial and error to create an oscillation the best I could in Excel. It will be very informative to see what happens in Mathematica when you enter your equations.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

I am guessing I still don't have this correct because the plots do not match. If you can find any mistakes that I have made then please point them out. I'm trying to reproduce your very first example.

If you can think of any little experiment we can try on simpler made-up systems then maybe that would confirm that simpler examples do match between Excel and Mathematica.

If I look at the equations for each row of your Excel sheet then I think I see

t+dt,((BX*MX/(1+KZ*zm)-xm/TX)*dt+xm)/dt,((BY*MY/(1+KX*xm)-ym/TY)*dt+ym)/dt,((BY*MY/(1+KX*ym)-zm/TY)*dt+zm)/dt

I think that is what I am trying to reproduce in Mathematica, with the translation from discrete to differential equation. But it still seems like I am misunderstanding something.

enter image description here

POSTED BY: Bill Nelson
Posted 3 months ago

Hi Bill, I think we're both having a lot of fun with this!

In your code above I believe there is a factor missing in each equation. For example, in the first equation (1+Kz) should be (1+Kz z[t]). The [z] in the spreadsheet graphic is a bit confusing. I think it's how chemists write "concentration of z."

Best, David

POSTED BY: David Keith
Posted 3 months ago

Bill,

David is correct. All the K parameters should be multiplied by their respective protein concentrations in all the differential equations.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Hi Scott,

No bother. I really enjoy working with Mathematica.

I suspect the problem you were having is that I was placing the equations within a list. I like putting them in a list referred to by a symbol (eqs) because it keeps things uncluttered. That works fine for NDSolve, but not for Manipulate. Manipulate needs for the symbols being manipulated to appear directly in the body of the command, or it does not see them. There is a work-around for that which I give in the attached notebook "manipulate example.nb."

In the second notebook I took your original code, slightly modified, and made a Manipulate from it. It did not need the work-around because I put the equations directly in the body.

This is fun. Let me know if you have more questions, or if you encounter issues.

POSTED BY: David Keith
Posted 3 months ago

Thank you for all your expertise and help! I am glad it is fun for you. Not so much for me yet. Hopefully, someday I will get there. I will take a look at the notebook in an hour or two. I am really glad you engaged in this help request.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Thank you, Closer. Still not the same, but this looks almost the same as what David posted 21 hours ago

enter image description here

POSTED BY: Bill Nelson
Posted 3 months ago

David and Bill,

Here is a Youtube link showing the method (Euler's) I used to solve those three differential equations in Excel. https://www.youtube.com/watch?v=vXWb1Kspx8s I think you surmised this already, Bill, when you deconstructed my Excel equations. Euler's method is crude and slow, but I always thought if the dt was small enough, it approximated derivatives pretty accurately (hence my small time increment of 0.000025 min. and my 1 million row Excel sheet).

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Bill and David,

You both are mathematicians, phsicists, or engineers I believe. I am surprised Euler's method with such a small time increment used in Excel is giving such a different result from the numerical solution in Mathematica. Is this to be expected mathematically? It seems a little suspicious to me, but I may not be understanding some deep mathematical truth of what is going on here.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

With that fine a time step and reasonably well behaved equations I doubt that the problem is in the accuracy of Euler's method.

POSTED BY: David Keith
Posted 3 months ago

David,

That was exactly my thinking, so I am still dumbfounded in the differences between Excel and Mathematica. Can you see a problem in my truncated Excel sheet with how I implemented Euler's method? Of course, the first truncated sheet I attached a few days ago had a mistake in it, but the corrected one I attached yesterday should be right. Unless there is a mistake in my Excel sheet, I do not understand the discrepancy.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

I'll have a look. Although I find reading Excel is a bit difficult. I am looking at doing Eulers method with Mathematica using your time step.

POSTED BY: David Keith
Posted 3 months ago

David,

Excellent idea. I would have done it, but I do not know how to set up Mathematica to do a repetitive calculation like that. It will be informative for me to look at your notebook to learn how to do this once you have completed it.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

David,

To save you time looking for the corrected truncated Excel sheet, here it is again.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

David,

A very nice example! Thank you for putting this together for me.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

Hi Scott,

I implemented Euler's method in Mathematica using a function called NestList. It repeatedly applies a function to the last result, starting with some initial condition. I used your equations for the derivatives and your time step. The original result that both Bill and I have using NDSolve is this: enter image description here

The result using Euler's method is this: enter image description here

You can see that they are quite similar. And they converge on the same concentrations. I suspect the difference arises from the fixed time steps being to large in the early part of the simulation, resulting in under damping. Mathematica has adaptive time stepping to prevent that.

I don't know why the Excel Euler's method produces different results, but I would trust NDSolve over the perhaps-too-simple Euler's method.

Attachments:
POSTED BY: David Keith
Posted 3 months ago

Here is the result if the time step is reduced by a factor of 10. But it takes a long time to run. enter image description here

Clearly the time step is an issue.

POSTED BY: David Keith
Posted 3 months ago

David,

This is beautiful work and what I have been after for about 5 days now! To me it seems clear from your two simulations using Euler's method that one never knows when the time increment is small enough to ensure accuracy. One also runs into computational time problems in Excel with really, really small time increments. I guess I would have to go to 10 million rows in Excel to get the accuracy I need. Thank you again for all the time you put into solving this conundrum for me.

Do you mind sending me your notebook where you implemented Euler's method? I would like to know how to do that method in Mathematica for comparison purposes.

Scott

POSTED BY: Scott Klakamp
Posted 3 months ago

It’s attached in the post above and called “by Euler.nb”

I’m not planning to look at your Excel file. Clearly it has some error, but even corrected, Eulers method can’t compete with methods that include adaptive stepping.

The learning curve for Mathematica is somewhat steep. But there are good learning resources on the Wolfram site, and the benefits are substantial. It is a very capable and versatile tool.

Best, David

POSTED BY: David Keith
Posted 3 months ago

David,

Sorry that I missed the attachment.

This whole issue is exactly why I tried to do this in Mathematica to see if there were differences and there are. I wanted the most accurate answer I could get and Mathematica gives me that.

That Euler notebook in Mathematica looks complicated. I have some studying to do there!

I am satisfied my original question has been answered thanks to both Bill and you!

Scott

POSTED BY: Scott Klakamp
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract