Plotting in a loop in mathematica

Click For Summary

Discussion Overview

The discussion revolves around using Mathematica to solve a system of differential equations and plot the solution curves. Participants explore methods for implementing loops to automate the plotting process for varying parameters within the equations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks advice on creating a loop to plot solution curves for a system of differential equations with varying parameters.
  • Another participant suggests using the Table function to generate an array of solutions based on the parameters, followed by a loop for plotting.
  • A participant expresses gratitude for the suggestion and reports success in generating solutions and graphs with multiple parameters.
  • Another participant introduces a separate problem involving two lists (temperature and time) and their correspondence, seeking advice on how to process them together.
  • A response provides an example of using a Do loop to iterate through the elements of the two lists, referencing their indices.
  • Another participant mentions the possibility of dynamically joining sets and iterating through lists using functions that handle unions.

Areas of Agreement / Disagreement

Participants generally agree on the utility of using loops in Mathematica for solving and plotting, but the discussion introduces a separate topic regarding list processing, which remains unresolved.

Contextual Notes

There are limitations regarding the specific implementation details of the loops and the handling of the lists, as well as the dependence on the structure of the input data.

mummerta
Messages
4
Reaction score
0
I am trying to solve a system of differential equations and plot solution curves. The differential equations have parameters. I would like to write a loop for these parameters and have the solution curves plot.

Here is my code for the solving / plotting. The parameters I want to change are are currently 7 and 4.

sol = NDSolve[{x'[t] == -7*x[t] y[t] + 4 y[t],
y'[t] == 7*x[t] y[t] - 4 y[t], x[0] == 50, y[0] == 10}, {x,
y}, {t, 0, 10}]
Plot[Evaluate[x[t] /. sol], {t, 0, 1}, PlotRange -> All]
Plot[Evaluate[y[t] /. sol], {t, 0, 1}, PlotRange -> All]
ParametricPlot[Evaluate[{x[t], y[t]} /. sol], {t, 0, 10},
PlotRange -> All]
{x[10], y[10]} /. sol

I don't know how to create a loop (or even which type of loop to use) that will output a plot for each new parameter value.

Any advice that you have to offer would be much appreciated.
 
Last edited:
Physics news on Phys.org
Use the Table[..] function to do the loop.

First use Table[NDSolve[...],{i,Length[arr]}] to get an array of solutions for the parameters you want, where 'arr' is an array storing your parameters.

Then just do a similar loop for your plot. ie. Table[Plot[...],{i,Length[solArr]}], where solArr is the array of solutions that would have been returned from the previous line.
 
Thanks so much! I never would have thought to use a Table.

I now have solutions and graphs running with 4 parameters!
 
Hi, I have a problem with mathematica...I have two lists ...one is for temperature and the other is for time...and they are related..that is...each element in time list corresponds to temperature on the other list...I have a huge formula where i need to use corresponding elements from both lists... any suggestions ?
 
If your two list are in list format such as:

Time = {1, 2, 3, 4} and Temp = {4, 5, 6, 7}

with the correspondence between
(Time, Temp)
(1,4)
(2,5)
(3,6)
(4,7)

That is Time[] corresponds to Temp[]

Then you can use a Do loop in the following way:

Do[ put commands here separated by semicolons referencing i , {i,1,4} ]

For example

Do[Print[Time[]*Temp[];
Print[Time[]+Temp[], {i,1,4}]
 
You can join the sets dynamically. You can create a union. And most functions will iterate the lists, either as a union or indivdually.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K