Solving second order ODE with ode45

3 views (last 30 days)
Hey there, I'm trying to solve this ODE: y''=(x*y' - (x^2-n^2)*(1/x) (while n is a global variable and y(0.5)=1, y'(0.5)=1) using this code:
function [x,y] = solve(n)
global n_global;
n_global=n;
syms y(x);
[x, Y]= ode45(@odefun, [0.5, 10], [1 1]);
figure;
plot(x,Y);
function dy_dx = odefun(x,y)
global n_global;
dy_dx=zeros(2,1);
dy_dx(1)=y(2);
dy_dx(2)=(1/x)*(x*y(2)-(x^2-n^2));
and something does not working right, can someone help me out here?

Accepted Answer

Torsten
Torsten on 5 Jan 2018
Since you start at x=0, you divide by zero in the line
dy_le_dx(2)=(1/x)*(x*y(2)-(x^2-n^2));
Best wishes
Torsten.
  5 Comments
Torsten
Torsten on 5 Jan 2018
dy_dx(2)=(1/x)*(x*y(2)-(x^2-n_global^2));
Best wishes
Torsten.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!