euler t t = Columns 1 through 3 0 0.300000000000000 0.600000000000000 Columns 4 through 6 0.900000000000000 1.200000000000000 1.500000000000000 Columns 7 through 9 1.800000000000000 2.100000000000000 2.400000000000000 Columns 10 through 11 2.700000000000000 3.000000000000000 euler {Error: File: ode1.m Line: 5 Column: 4 This statement is not inside any function. (It follows the END that terminates the definition of the function "ode1".) Error in euler (line 9) u(i+1) = u(i) + h * ode1( u(i), t(i) ); } euler plot(t,u) plot(t,u) euler plot(t,u) help ode45 ode45 Solve non-stiff differential equations, medium order method. [TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y' = f(t,y) from time T0 to TFINAL with initial conditions Y0. ODEFUN is a function handle. For a scalar T and a vector Y, ODEFUN(T,Y) must return a column vector corresponding to f(t,y). Each row in the solution array YOUT corresponds to a time returned in the column vector TOUT. To obtain solutions at specific times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. [TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0,OPTIONS) solves as above with default integration properties replaced by values in OPTIONS, an argument created with the ODESET function. See ODESET for details. Commonly used options are scalar relative error tolerance 'RelTol' (1e-3 by default) and vector of absolute error tolerances 'AbsTol' (all components 1e-6 by default). If certain components of the solution must be non-negative, use ODESET to set the 'NonNegative' property to the indices of these components. ode45 can solve problems M(t,y)*y' = f(t,y) with mass matrix M that is nonsingular. Use ODESET to set the 'Mass' property to a function handle MASS if MASS(T,Y) returns the value of the mass matrix. If the mass matrix is constant, the matrix can be used as the value of the 'Mass' option. If the mass matrix does not depend on the state variable Y and the function MASS is to be called with one input argument T, set 'MStateDependence' to 'none'. ODE15S and ODE23T can solve problems with singular mass matrices. [TOUT,YOUT,TE,YE,IE] = ode45(ODEFUN,TSPAN,Y0,OPTIONS) with the 'Events' property in OPTIONS set to a function handle EVENTS, solves as above while also finding where functions of (T,Y), called event functions, are zero. For each function you specify whether the integration is to terminate at a zero and whether the direction of the zero crossing matters. These are the three column vectors returned by EVENTS: [VALUE,ISTERMINAL,DIRECTION] = EVENTS(T,Y). For the I-th event function: VALUE(I) is the value of the function, ISTERMINAL(I)=1 if the integration is to terminate at a zero of this event function and 0 otherwise. DIRECTION(I)=0 if all zeros are to be computed (the default), +1 if only zeros where the event function is increasing, and -1 if only zeros where the event function is decreasing. Output TE is a column vector of times at which events occur. Rows of YE are the corresponding solutions, and indices in vector IE specify which event occurred. SOL = ode45(ODEFUN,[T0 TFINAL],Y0...) returns a structure that can be used with DEVAL to evaluate the solution or its first derivative at any point between T0 and TFINAL. The steps chosen by ode45 are returned in a row vector SOL.x. For each I, the column SOL.y(:,I) contains the solution at SOL.x(I). If events were detected, SOL.xe is a row vector of points at which events occurred. Columns of SOL.ye are the corresponding solutions, and indices in vector SOL.ie specify which event occurred. Example [t,y]=ode45(@vdp1,[0 20],[2 0]); plot(t,y(:,1)); solves the system y' = vdp1(t,y), using the default relative error tolerance 1e-3 and the default absolute tolerance of 1e-6 for each component, and plots the first component of the solution. Class support for inputs TSPAN, Y0, and the result of ODEFUN(T,Y): float: double, single See also ode23, ode113, ode15s, ode23s, ode23t, ode23tb, ode15i, odeset, odeplot, odephas2, odephas3, odeprint, deval, odeexamples, rigidode, ballode, orbitode, function_handle. Reference page in Help browser doc ode45 ode45( @ode1, [0 3], 1 ) ode45( @ode1, [0 3], 1 ) hold on plot(t,u,'+r') feuler( @ode1, 1, 0, 3, 10 ) ans = Columns 1 through 3 1.000000000000000 0.700000000000000 -0.618443691672424 Columns 4 through 6 -0.549083037934104 0.211793004434057 0.274492599267961 Columns 7 through 9 -0.121799308067884 -0.187443606786559 0.030376076322544 Columns 10 through 11 0.094278448793071 -0.015033202622791 [fu, ft ] = feuler( @ode1, 1, 0, 3, 1000 ); plot( ft, fu, 'b') close all plot( ft, fu, 'b') hold Current plot held hold on ode45( @ode1, [0 3], 1 ) [fu, ft ] = feuler( @ode2, exp(1), 1, 2, 10 ) fu = Columns 1 through 3 2.718281828459046 3.533766376996759 4.539773443530052 Columns 4 through 6 5.774499187659123 7.282994814895872 9.118241839096839 Columns 7 through 9 11.342387458469149 14.028162191422943 17.260505010152450 Columns 10 through 11 21.138424678639183 25.777129906895595 ft = Columns 1 through 3 1.000000000000000 1.100000000000000 1.200000000000000 Columns 4 through 6 1.300000000000000 1.400000000000000 1.500000000000000 Columns 7 through 9 1.600000000000000 1.700000000000000 1.800000000000000 Columns 10 through 11 1.900000000000000 2.000000000000000 [fu, ft ] = feuler( @ode2, exp(1), 1, 2, 1000 ); plot( ft, fu, 'r') hold on ode45( @ode2, [1 2], exp(1) ) [fu, ft ] = feuler( @ode2, exp(1), 1, 2, 100 ); [fu, ft ] = feuler( @ode2, exp(1), 1, 2, 100 ); ode45( @ode2, [1 2], exp(1) ) hold on plot( ft, fu, 'r') close all ode45( @ode2, [1 4], exp(1) ) [fu, ft ] = feuler( @ode2, exp(1), 1, 4, 100 ); hold on plot( ft, fu, 'r') [fu, ft ] = feuler( @ode2, exp(1), 1, 4, 1000 ); plot( ft, fu, 'g') target = -0.037822634055742 target = -0.037822634055742 [fu, ft ] = feuler( @ode1, 1, 0, 3, 10 ); abs( target - fu(end) )/abs(target) ans = 0.602534223273937 [fu, ft ] = feuler( @ode1, 1, 0, 3, 100 ); abs( target - fu(end) )/abs(target) ans = 0.001946729465011 [fu, ft ] = feuler( @ode1, 1, 0, 3, 1000 ); abs( target - fu(end) )/abs(target) ans = 7.272437623209290e-04 [fu, ft ] = feuler( @ode1, 1, 0, 3, 10000 ); abs( target - fu(end) )/abs(target) ans = 7.799831062937499e-05 [fu, ft ] = feuler( @ode1, 1, 0, 3, 100000 ); abs( target - fu(end) )/abs(target) ans = 7.852521557373846e-06 [fu, ft ] = feuler( @ode1, 1, 0, 3, 10000000 ); abs( target - fu(end) )/abs(target) ans = 7.858365598684429e-08 diary off