Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Octave Notebook for Running ode45 example

27 views
Kernel: Octave
example_ode45_octave
txt = Displaying data Time X Y Z data = 0.00000 0.00000 0.50000 3.00000 0.06813 0.55407 0.83645 2.62494 0.17032 1.20015 1.22179 2.21842 0.32032 1.89359 1.62535 1.97279 0.47032 2.46767 1.95752 2.09086 0.62032 3.08426 2.32506 2.55575 0.77032 3.90002 2.83145 3.47739 0.92032 5.14319 3.62799 5.22076 1.07032 7.28255 5.02648 8.83269 1.22032 11.56961 7.86064 17.84151 1.37032 22.57562 15.17743 49.19559 1.47406 45.53440 30.47076 142.07192 1.50000 57.38426 38.36774 201.32018 txt = Fixed spaced data Time X Y Z data = 0.00000 0.00000 0.50000 3.00000 0.25000 1.56849 1.43616 2.08795 0.50000 2.58966 2.03023 2.18284 0.75000 3.78949 2.76284 3.35252 1.00000 6.27957 4.37084 7.13934 1.25000 13.74711 9.30824 24.04480 1.50000 57.38426 38.36774 201.32018
Image in a Jupyter notebookImage in a Jupyter notebook
% function for the derivative, this example has three equations in % three unknowns as x = [x(1), x(2), x(3)] for the dependent variables f = @(t,x) [-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)]; % calling ode45 to find a solution to the diff. eq'n % over independent t variable from 0 to 1.5 tstart = 0; tend = 1.5; % with initial condition [0 1/2 3] % xa is the 'x approximate' at the values of t, % xa has the 3-components of the x vector as columns, each row for a time [t,xa] = ode45(f,[tstart tend],[0 1/2 3]); % displaying output as a plot figure(1) plot(t,xa(:,2)) title('y(t)') xlabel('t'), ylabel('y') % displaying output as a plot figure(2) plot(t,xa(:,1)) title('x(t)') xlabel('t'), ylabel('x') % output a header txt = sprintf('Displaying data\n Time X Y Z') % displaying output as a table data = [t,xa(:,1), xa(:,2), xa(:,3)] % Note: xa has 3-components of the x vector as columns, each row for a time % this data does not have fixed time-step spacing % use interpolation to get 'nicer' output data table h=0.25; % the fixed step by h for desired output: t0 = (tstart:h:tend)'; % t0 is array of times at step-h spacing xa0 = interp1(t,xa,t0); %xa0 now has data at spacing from t0 %output nicer table % output a header txt = sprintf('Fixed spaced data\n Time X Y Z') % displaying output as a table data = [t0,xa0(:,1), xa0(:,2), xa0(:,3)]
txt = Displaying data Time X Y Z data = 0.00000 0.00000 0.50000 3.00000 0.06813 0.55407 0.83645 2.62494 0.17032 1.20015 1.22179 2.21842 0.32032 1.89359 1.62535 1.97279 0.47032 2.46767 1.95752 2.09086 0.62032 3.08426 2.32506 2.55575 0.77032 3.90002 2.83145 3.47739 0.92032 5.14319 3.62799 5.22076 1.07032 7.28255 5.02648 8.83269 1.22032 11.56961 7.86064 17.84151 1.37032 22.57562 15.17743 49.19559 1.47406 45.53440 30.47076 142.07192 1.50000 57.38426 38.36774 201.32018 txt = Fixed spaced data Time X Y Z data = 0.00000 0.00000 0.50000 3.00000 0.25000 1.56849 1.43616 2.08795 0.50000 2.58966 2.03023 2.18284 0.75000 3.78949 2.76284 3.35252 1.00000 6.27957 4.37084 7.13934 1.25000 13.74711 9.30824 24.04480 1.50000 57.38426 38.36774 201.32018
Image in a Jupyter notebookImage in a Jupyter notebook