function rk % Numerical solutions and plots using Runge-Kutta implementation % This command sets options for the ode routine below. % for other options, type "help ode45", options = odeset ('RelTol', 1.0e-06); % We use a loop here to get solutions for different initial data. % in this case, x0 ranges from -2 to 2 in increments of .1 % YOU WILL NEED TO CHANGE THIS FOR EACH PROBLEM, OR YOUR PLOTS % MIGHT LOOK VERY STRANGE! x0small = -2; x0big = 2; for x0 = [x0small:0.1:x0big], % The next command invokes the fourth order runge-kutta scheme for the % ODE x' = odefunction. [0 5] specifies the time interval % over which it is solved, x0 is the initial data, % and "options" are specified above. % notice that we never set the timestep size: this is because % ode45 does it for us! [t,x] = ode45( @odefunction, [0 5], x0, options); % 't' is a vector of all t values % and the 'x' is a vector of all corresponding x values. % Therefore to plot the solution, we do the following: plot(t,x); hold on; %'hold' keeps the current plot visible next time 'plot' is used % look at the help menu or type 'help plot' to see many more % graphical features; for example, 'plot(t,x,'.') will plot points % instead of lines. end; %end the loop over initial conditions %now were done plotting, so........ xlabel('t'); % label axes ylabel('x'); axis([0 5 x0small x0big]); % only show the "relevant" part of the plot hold off; %this turns the 'hold' feature off %------------------------------------------------------------- % This defines the function f(x) in the equation dx/dt = f(x); % you will edit this for each problem function f = odefunction(t,x) f = -x^3 + x;