MATLAB: How can i solve y’=t/t^2+1 by programming a runge kutta 4’th order method

runge-kutta 4

i need a code

Best Answer

  1. As written, this can't be done, because there's a singularity at t = 0
  2. Assuming that you actually mean y' = t/(t^2 + 1)... why would you need RK4? The ODE can be solved by simple integration: y(t) = log(sqrt(t^2 + 1))
  3. If you really really want to do it numerically:
% Define ODE rate equation
ratefun = @(t,y) t/(t^2+1);
% Solve for t in [0,1] with initial condition y(0) = 0
[t,y] = ode45(ratefun,[0,1],0);
% View solution
plot(t,y,t,log(sqrt(t.^2+1)))
legend('RK45','exact','Location','NW')
But... ode45 is a variable-step RK45 integrator, not the classical fixed-step RK4 method. If you really want the fixed-step RK4 method for an equation with a simple analytic solution... well, then I have to assume that this is a homework problem.
If this is a homework problem, please do not just post a request for the solution. Show what you have done, and ask a specific question for guidance on a particular part of the problem.