Solving coupled ODEs with Matlab

In summary, to solve coupled ODEs with Matlab, you can use the bvp4c function and pass a second parameter containing the values of the other equation. You can also try using the "events" option or the "ode15s" solver to handle changing vector lengths.
  • #1
mika
3
0
I was wondering how coupled ODEs could be solved with Matlab.. I'm having trouble passing the coupling solution vectors between solvers since the length of the vectors isn't constant during the iteration e.g.

sol1 = bvpinit(linspace(0.,h,200),[0.1 0]);
sol2 = bvpinit(linspace(0.,h,200),@mat4init);

f=sol1.y
g=sol2.y

sol1 = bvp4c(@fODE,@fBC,sol1,options1,g,Re,K);
sol2 = bvp4c(@gODE,@gBC,sol2,options1,f,Re,K);


function dydx = fODE(x,y,g,Re,K)

dydx = [ y(2,:)
Re*(y(1,:).^2-g.*2+K^2)];

function dgdx = gODE(x,y,g,Re,K)

dgdx = [ g(2,:)
2*Re*(y(1,:).*g(1,:))];

this doesn't work bacause the dimensions of y and g don't match during iteration. Any ideas how to overcome this?
 
Physics news on Phys.org
  • #2
The best way to solve coupled ODEs with Matlab is to use the built-in function bvp4c. This function allows you to pass a second parameter (in addition to the ODE and boundary condition) containing the values of the other equation that need to be used in the current iteration. For example, if you have two coupled ODEs, you can call bvp4c like this:

sol1 = bvp4c(@fODE,@fBC,sol1,options1,g,Re,K);
sol2 = bvp4c(@gODE,@gBC,sol2,options1,f,Re,K);

where f and g are the solution vectors from the previous iteration, and Re and K are parameters for both equations. The bvp4c function will then take care of passing the correct values between the two equations.
 
  • #3
Hi there,

I've encountered a similar issue before when solving coupled ODEs with Matlab. One thing you can try is to use the "events" option in the bvp4c solver to specify a specific event that will trigger a change in the length of the vectors. For example, you can set an event that checks if the length of the vectors has reached a certain value and if so, it will trigger a change in the initial conditions for the next iteration. You can also try using a "while" loop to continuously update the initial conditions until the desired length is reached.

Another approach you can try is to use the "ode15s" solver instead of bvp4c. This solver is specifically designed for stiff systems of ODEs and may handle the changing length of the vectors better.

I hope this helps. Let me know if you have any other questions or if these suggestions work for you. Good luck with your problem!
 

1. How do I define the coupled ODEs in Matlab?

In Matlab, coupled ODEs can be defined using the function "odefun" which takes in the independent variable, dependent variables, and any parameters as inputs. This function should return a vector of the derivatives of the dependent variables.

2. What is the syntax for solving coupled ODEs in Matlab?

The syntax for solving coupled ODEs in Matlab is: [T,Y] = ode45(odefun,tspan,y0), where odefun is the function that defines the ODEs, tspan is the time interval, and y0 is the initial conditions for the dependent variables. The output T is a vector of time values and Y is a matrix of the corresponding values of the dependent variables at each time step.

3. How do I plot the results of the coupled ODEs in Matlab?

To plot the results, you can use the "plot" function in Matlab. For example, if you want to plot the first dependent variable "y1" against the independent variable "t", you can use the code: plot(T,Y(:,1)). This will plot the values of "y1" at each time step.

4. Can I solve stiff coupled ODEs in Matlab?

Yes, Matlab has built-in solvers for stiff ODEs such as "ode15s" and "ode23s". These solvers are more suitable for stiff systems as they can handle large differences in the timescales of the dependent variables.

5. How can I improve the accuracy of my results when solving coupled ODEs in Matlab?

One way to improve accuracy is to decrease the step size used by the solver. This can be done by specifying a smaller value for the "MaxStep" option in the solver function. Additionally, you can try using a higher order solver such as "ode113" which can provide more accurate results at the cost of longer computation time.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
565
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
0
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
823
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
819
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top