-4

The differential equation is:

k*r=sqrt( (r`)^2 + r^2 )

The value of k is 2. How can I realize the function and solve it with ode45?

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38

1 Answers1

0

This is pretty straightforward. A few important things:

  1. You need to express your differential equation in the format that ODE45 expects, which is dy/dt = f(t,y). That means that for your function, rather than k*r=sqrt( (drdt)^2 + r^2 ), you need to consider this as drdt = sqrt( (k*r)^2 - r^2 ) (assumming that I did my algebra correctly).

  2. The function needs to be vectorizable. For this problem, that pretty much means that instead of ^, use .^, and instead of *, use .*.

Then we can use basic anonymous function syntax to setup the function to be solved:

k = 2;
fn_drdt = @(t,r) sqrt( (k.*r).^2 - (r.^2)  );

Use ODE45 to solve it

tStart = 0;
tEnd = 10;
r_init = -5;
[t, r] = ode45(fn_drdt, [tStart tEnd], r_init);

And then make a basic plot

plot(t,r,'.');

And add a title

title(sprintf('Solution to %s with k=%d ,r_0=%d', func2str(fn_drdt), k, r_init), 'fontname','courier')
Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • `title(sprintf('Solution to %s with k=%d ,r_0=%d', func2str(fn_drdt), k, r_init), 'fontname','courier')` – Eitan T Jan 15 '13 at 08:54
  • How can i go through on this curve with a little circle? – user1839790 Jan 15 '13 at 10:32
  • i tried this but dont works:for i=0:0.1:length(t) plot(i,r(i),'ro'); pause(0.02); end – user1839790 Jan 15 '13 at 10:34
  • 1
    @EitanT, that is better. I folded it into the answer. – Pursuit Jan 15 '13 at 16:23
  • @user1839790 "How to animate a plot" may be an interesting discussion for a separate question; or see if a few of these give you ideas: http://stackoverflow.com/search?tab=votes&q=[matlab]%20animate%20plot . – Pursuit Jan 15 '13 at 16:27
  • @Pursuit No problem. Have a +1 for the unappreciated effort :) – Eitan T Jan 15 '13 at 16:32
  • If this were me, guys, I wouldn't have answered the question. The poster has made zero attempt at answering his question. This was a dump for homework help and you gave it to him. It is unlikely the poster has actually learned anything from your answer. – Dang Khoa Jan 15 '13 at 20:47
  • I don't know. The question was specific and answerable, and I could answer it almost as fast as I could find another answer. (FWIW, the closest existing answer is probably: http://stackoverflow.com/questions/3691692/solving-an-ode-using-matlab/3691794#3691794, although I like mine better, probably because it sounds like me). This seems to fall within the "go ahead and answer if you can" category described here: http://meta.stackexchange.com/a/81693. – Pursuit Jan 15 '13 at 23:21