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

In summary, to solve for the first differential equation, the Matlab code provided uses the function nlseq and the matrix manipulation involving R, L, and V. To incorporate a second differential equation, the state vector should include x1, x2, and w. The lnseq file should have dq(1:2) as x1' and x2', and dq(3) as w'. Setting specific times instead of a time range will return the values of x1, x2, and w at those times, which can then be used for matrix manipulation to plot y vs w. Alternatively, the function definition can include additional parameters.
  • #1
louie
10
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: 419
Physics news on Phys.org
  • #2
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.
 
  • #3
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...
 

1. What is the process for solving 2 differential equations in MatLab?

The process for solving 2 differential equations in MatLab involves using the "dsolve" function, which takes in the differential equations as inputs and returns the symbolic solutions. These solutions can then be used to plot the function.

2. How do I plot y vs w in MatLab?

To plot y vs w in MatLab, you can use the "ezplot" function. This function takes in the symbolic solutions from the "dsolve" function and plots them on a graph. You can also customize the graph by specifying the range and adding labels and titles.

3. Can I solve and plot more than 2 differential equations in MatLab?

Yes, MatLab allows you to solve and plot any number of differential equations. You simply need to provide all the equations as inputs to the "dsolve" function and then use the "ezplot" function to plot the solutions.

4. How do I add initial conditions to my differential equations in MatLab?

To add initial conditions to your differential equations in MatLab, you can use the "dsolve" function with the "initials" option. This allows you to specify the initial values for each variable in the equations, which will be used to find the particular solutions.

5. Can I use MatLab to solve and plot differential equations with complex solutions?

Yes, MatLab has the ability to handle complex numbers and can solve and plot differential equations with complex solutions. You can use the "dsolve" function with the "complex" option to specify that the solutions should be in the form of complex numbers.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
937
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top