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?

 채택된 답변

Torsten
Torsten 2018년 1월 5일

0 개 추천

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

Ran Cohen
Ran Cohen 2018년 1월 5일
편집: Ran Cohen 2018년 1월 5일
Sorry - x starts at 0.5. I've edit it.
Torsten
Torsten 2018년 1월 5일
And what is the problem ?
"something does not working right" is quite vague.
Best wishes
Torsten.
Ran Cohen
Ran Cohen 2018년 1월 5일
편집: Ran Cohen 2018년 1월 5일
those are the errors that I get:
Undefined function or variable 'n'.
Error in solve>odefun (line 15) dy_dx(2)=(1/x)*(x*y(2)-(x^2-n^2));
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in solve (line 5) [x, Y]= ode45(@odefun, [0.5, 10], [1 1]);
dy_dx(2)=(1/x)*(x*y(2)-(x^2-n_global^2));
Best wishes
Torsten.
Ran Cohen
Ran Cohen 2018년 1월 5일
Thanks mate!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2018년 1월 5일

댓글:

2018년 1월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by