I have differential equations and I need to write them into a vector function
23 views (last 30 days)
William Rose on 17 Nov 2021
Edited: William Rose on 18 Nov 2021
The documentation for ode45() is really good, I think. Can you make an effort at writing some code, and post what you have so far? You already have both first order differential equations, which means you don;t have to do any more math.
A key concept for this kind of problem is that you define a column vector whose elements are the variables for which you have a first order differential equation. In your problem, you could define the column vector x of length 2, where x(1)=the variable you call y, and x(2)=the variable you call z.
You write a function that receives the current value of x and of time t. It returns the column vector of first derivatives of x. You can save the function as its own .m file, or you can include it at the end of your code, after the last line of code. The ode45() documentation decribes all this. YOur function will look like this:
function dxdt = odefun(t,x)
% x (2x1): x(1)=y, x(2)=z
% t (scalar)
% dxdt (2x1)
dxdt=zeros(2,1); %make sure dxdt is a column and not a row
dxdt(1)=5 + x(2)/5 - 4*x(1)/(20+3*t);
dxdt(2)=4*x(1)/(20+3*t) - 2*x(2)/5;
You'll also need a main program which calls ode45. Again, see the docmentaiton for good examples.