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?
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?
This is pretty straightforward. A few important things:
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).
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')