Solving 2 D.Es and Plotting y vs w in MatLab

  • Context: MATLAB 
  • Thread starter Thread starter louie
  • Start date Start date
  • Tags Tags
    Matlab Plotting
Click For Summary
SUMMARY

The discussion focuses on solving two differential equations (D.E.s) using MATLAB, specifically with the function 'ode23'. The provided code snippet demonstrates how to set up the first D.E. and suggests incorporating a second D.E. by extending the state vector to include the variable 'w'. The user seeks guidance on modifying the code to handle both equations and plotting the results of 'y' versus 'w'. Key recommendations include defining the state vector as [x1, x2, w] and using matrix manipulation for plotting.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with differential equations and their numerical solutions
  • Knowledge of matrix operations in MATLAB
  • Experience with MATLAB's 'ode23' function for solving ODEs
NEXT STEPS
  • Learn how to define and manipulate state vectors in MATLAB
  • Research advanced plotting techniques in MATLAB for visualizing multiple variables
  • Explore the use of 'ode45' as an alternative solver for differential equations
  • Investigate MATLAB's function handling to pass additional parameters in ODE solvers
USEFUL FOR

Mathematics students, engineers, and researchers working with differential equations and numerical methods in MATLAB.

louie
Messages
10
Reaction score
0
Please refer to jpeg extension

I think that the following MatLab code will solve the first D.E,

>> function xdot = nlseq(t,x); % Returns the state derivative
>> R = [2 -1; 3 5];
>> L = [1 – cos(0.5*t) 5*sin(4*t); 20*sin(2*t) 3–cos(0.8*t)];
>> V = [15*sin(t); 25*cos(t)];
>> xdot = inv(L)*(V – R*X);

>> tspan = [0, 10]; % Time interval
>> x0 = [0;0]; % Initial condition
>> [t, x] = ode23(‘nlseq’,tspan,x0);


I would like to know how to incorporate the second D.E into the above code so that I can solve for both (I realize I need to make another xdot command, but I’m not sure how to do it).
Another problem that I have is that I want to plot y vs w, where w is the solutions from the second D.E and y is shown on the jpeg extension,

So basically I want to take my solution (at every interval) from solving the first D.E. and transform it through the matrix shown on the extension. I would think that I need to write some kind of loop, but I’m not sure how to do it.

Any help with the above problems would be greatly appreciated
 

Attachments

  • matlab.jpg
    matlab.jpg
    11.7 KB · Views: 511
Physics news on Phys.org
To do it all in one shebang, do the following:

Have the state vector be: [x1,x2,w]. For clarity, I'll call it q, with the derivative being dq

In your lnseq file, have dq(1:2) be your x1' and x2', and have dq(3) be w'

If you specify specific times instead of a time range ( [0:.1:10] instead of [0,10]) this will return the values of x1, x2, and w at those times.

In your main function, you can then do the matrix manipulation for each time to get the graph of y1 and y2, since w will be returned for the same times as x1 and x2.
 
Alternately, you could set the function definition as:

lnseq(t,x,flag,value1,value2,...)

and have the ode23 call read:

ode23('lnseq',t,w,[],variable1,variable2,...)

The empty bracket is used for precision options of ode23. The trailing variables are other parameters you want available in the function definition.

I think the first way will be better, though...
 

Similar threads

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