Matlab- solving odes for a matrix

In summary, to get a matrix of 85x200 values for z and r, you will need to create a loop that runs the ode solver with different values of H. The results can be accessed in the matrices z_results and r_results.
  • #1
birdhen
35
0
Hi there,

I have yet another Matlab query.
I have an m=file that I am using to solve an ode.
This file has a constant (call it H) within the differential and when I use the ode solver in the command window returns values of z and r (this returns two 85x1 vectors which can be plotted against each other).
Now I want to make H vary between 4200 and 4400 and get 85 values of z and r for every step in H, i.e. I want to get a matrix of 85x200 for each of z and r.
I am sorry if this is not worded very well, I am a bit confused as I am a Matlab novice, if anyone could give me some tips that would be much appreciated.

Cheers
 
Physics news on Phys.org
  • #2
To do this, you will need to create a loop that takes the value of H and runs the ode solver with each value of H. The code should look something like this:

for i = 4200:4400
% Set value of H
H = i;

% Run ode solver
[z, r] = ode45(@your_ode_file, tspan, y0);

% Save results
z_results(:,i) = z;
r_results(:,i) = r;
end

Once the loop is complete, you can access the results in the matrices z_results and r_results.

Hope that helps!
 
  • #3


I would suggest using a for loop to vary the value of H and store the resulting z and r values in a matrix. This can be achieved by creating a vector of values for H (e.g. H = 4200:1:4400) and then using a for loop to iterate through each value of H. Within the loop, you can use the ode solver to solve for z and r and store them in a matrix using indexing. For example, if your matrix for z and r is called "zr_matrix", the code within the loop could look something like this:

for i = 1:length(H)
% solve ode for z and r using H(i)
[z, r] = ode_solver(H(i));

% store z and r values in matrix
zr_matrix(:,i) = [z r];
end

This will result in a matrix with 85 rows and 200 columns, where each column represents the z and r values for a specific value of H. I hope this helps and feel free to reach out if you have any further questions.
 

1. How do I solve an ordinary differential equation (ODE) system using Matlab?

To solve an ODE system in Matlab, you will need to define the system of equations using symbolic variables, use the "odefun" function to create a function handle for the system, and then use the "ode45" function to numerically solve the equations. You can also use other ODE solvers such as "ode23" or "ode15s" depending on your specific problem.

2. Can I solve ODEs for a matrix in Matlab?

Yes, Matlab has the capability to solve ODEs for matrices. You can define the system of equations using matrix operations and use the "odefun" function to create a function handle for the system. Then, you can use the "ode45" function to numerically solve the equations for the matrix.

3. How can I specify initial conditions for my ODE system in Matlab?

You can specify initial conditions for your ODE system in Matlab by providing a vector of initial values to the "ode45" function. These initial values should correspond to the variables in your system of equations.

4. What is the format for defining a system of ODEs in Matlab?

The format for defining a system of ODEs in Matlab is as follows: dY/dt = f(t,Y), where Y is a vector of dependent variables, t is the independent variable, and f(t,Y) is a function that describes the rate of change of Y with respect to t. This function can be defined using symbolic variables or matrix operations.

5. How can I plot the results of my ODE system in Matlab?

To plot the results of your ODE system in Matlab, you can use the "plot" function and provide the time vector and the corresponding values of your dependent variables. You can also plot multiple solutions on the same graph by using the "hold" function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
680
  • Calculus and Beyond Homework Help
Replies
3
Views
311
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
3
Views
1K
Back
Top