Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I need to numerically solve $$\frac{\mathrm dx}{\mathrm dt} = y - x^3$$ $$\frac{\mathrm dy}{\mathrm dt} = -x - y^3$$
with initial conditions $x_0,y_0 = 1,0$ for $t$ from $1$ to $100$. I don't know how to do this.

I'd also like to plot this along with some other functions and don't know how to plot multiple functions on one plot. The specific functions are $$A = x^2 + y^2$$

and $$B = 1/t$$

I'm quite new to MATLAB, so I need some help.

share|improve this question
2  
You should be able to adapt the answer I gave you here to your current problem. –  J. M. Aug 15 '11 at 8:58
1  
As for plotting... –  J. M. Aug 15 '11 at 9:00
    
I don't see how in the first case. I tried dy(1) = y(2) - y(1).^3, dy(2) = - y(1) - y(2).^3. That didn't work. –  rapidash Aug 15 '11 at 9:29
    
"That didn't work." isn't very informative for me. What did you enter, and what did MATLAB spit out? –  J. M. Aug 16 '11 at 3:14

1 Answer 1

Let $z = [x,y]$, observe $z' = [x',y'] = [z(2)-z(1)^3,-z(1)-z(2)^3]$.
Your initial condition is $z_0 = [x_0,y_0] = [1,0]$

The trick is now we have a vector $z$ and we know its derivative $z'$ and initial values, we can certainly solve it (numerically).

Matlab solvers like ode45 or ode23 can easily tackle this, or you can write your own solver using Euler's method and so on.

Example Code

f = @(t,z)[z(2)-z(1)^3,-z(1)-z(2)^3];
[t z] = ode45(f,[1 100],[1 0]);

Remember now your $z$ vector contains both $x$ and $y$.
$z(:,1)$ will give you $x$ vector in correspondence to $t$ vector and $z(:,2)$ will give the $y$ vector

You can for example
plot(t,z(:,1))
to see how x evolve along t or
plot(t,z(:,2))
to see how y evolve along t. Or
plot(z(:,1),z(:,2))
to see the phase plot.


Regarding plotting against $A$ and $B$, I think you might have misunderstood the problem. We have x(t) and y(t) and we solved them, this is different from solving z(x,y,t). Don't get them confused.

Your A(x(t),y(t)) is basically a parametrized curve in 2 dimension (z against t). You can plot by

A = @(x,y) x.^2+y.^2;
plot(t,A(z(:,1),z(:,2));

And your B has nothing to do x(t) and y(t), it only depends on t, so again a 2 dimension plot.
B = @(t) 1./t;
plot(t,B(t));

To plot them on the same graph you can do either
plot(t,A(z(:,1),z(:,2),t,B(t));
or
plot(t,A(z(:,1),z(:,2)); hold on; plot(t,B(t)); hold off;

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.