% COMPARE.m % Sample script for comparing a nonlinear oscillator and % the linearized approximation. % Note, the system parameters(damping spring constants etc. % are defined in the m-file damped2.m). % % This file may be used for a quick demo. But is was % originally intended to provide help with the syntax: % After the first execution of this script, users may % want to copy (to the command window), and edit % specific commands from this file. Alternatively, % delete all unneeded commands, customize the script, % and save the file under a new name. % % All rights reserved. Matthias Kawski. April 1999. % Set the search path to include the working directory % where the user has write-permission, and where this % file and the file damped2.m reside on a local drive. % addpath('C:\temp') edit compare % % Edit the m-file that contains a description of the % differential equations. This is the place to modify % the system parameters, e.g. the damping constants. % edit damped2.m % % Define the initial conditions -- the same for both systems. % y0 is the initial position. y1 is the initial velocity. % y0=0 y1=1 % % Define a suitable time interval. % t0=0 tfin=20 % % Call the numerical DE-solver and write the results in % column format to the variables T and Y. To be safe first % clear the variables. % clear T clear Y [T,Y]=ode45('damped2',[t0,tfin],[y0,y1,y0,y1]); % % Take a look at the size of the data generated. T contains % a single column, Y contains 4 columns of the same length % size(T) size(Y) % % Plot the computed positions of the nonlinear (red) and of % the linear model (blue). Overlay also the velocities of the % nonlinear (magenta) and linear (green) models. % First clear the graphing window % clf plot(T,Y(:,1),'r-',T,Y(:,3),'b-',T,Y(:,2),'m:',T,Y(:,4),'g:')