ODE analytical solution

4 次查看(过去 30 天)
John
John 2011-3-26
How can I find the analytical solution to the following ode?
y'+ x/y=0
when x=0 and y=5.

采纳的回答

bym
bym 2011-3-26
simplify(dsolve('Dy=-x/y','y(0)=5','x'))
ans =
(25 - x^2)^(1/2)
  1 个评论
John
John 2011-3-27
From my text book I have coded Euler's Method to solve ODEs but i am confused on where to input each into the program the code I have is
function [t,y] = eulode(dydt, tspan, y0, h)
%eulode: Euler ODE solver
% [t,y] = eulode(dydt, tspan, y0, h, p1, p2,...)
% ` uses EULER'S method to INTEGRATE an ODE
% (uses the slope at the beginning of the stepsize to graph the
% function.)
%Input:
% dydt = name of hte M-file that evaluates the ODE
% tspan = [ti,tf] where ti and tf = initial and final values of
% independent variables
% y0 = initial value of dependent variable
% h = step size
% p1,p2 = additional parameter used by dydt
%Output:
% t = vector of independent variable
% y = vector of solution for dependent variable
if nargin<4, error('at least 4 input arguments required'), end
ti = tspan(1); tf = tspan(2);
if ~ (tf>ti), error('upper limit must be greater than lower limit'), end
t = (ti:h:tf)';
n = length(t);
%if necessary, add an additional value of t
%so that range goes from t=ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
t(n)=tf;
end
y = y0*ones(n,1); %preallocate y to improve efficiency
for i = 1:n-1 %implement Euler's Method
y(i+1) = y(i) + dydt(t(i),y(i))*(t(i+1)-t(i));
end
plot(t,y)
in my other m-file I am supposed to run the program with 3 different step sizes (.5; .1; .01) so my code looks like this
dydx=@(x,y) -(x/y);
[x1,y1]=eulode(dydx, [0 1],5,.5);
[x2,y2]=eulode(dydx,[0 1],5,.1);
[x3,y3]=eulode(dydx,[0 1],5,.01);
disp([x1,y1])
disp([x2,y2])
disp([x3,y3])
I think this is correct because each answer is displaying a value close to 5 (which the analytical value should be 5 since x=0)
Did i code this right?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by