machinename:~$ matlab >> >> diary first-try %creates a file named 'first-try' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % We solve the initial value problem for first order ode % % x' + x = 5sin(2t) % % x(0)=1 % % % First, we plot the directional fields for the equation, % then the solution curve corresponding to the initial data, % and finally we plot our computed solution: % y(t) = sin(2t)-2cos(2t) + 3exp(-t) % % Both curves must be exactly the same! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >> dfield % x'= -x + 5*sin(2*t) % you'll get a figure window, with an equation x'= x^2-t. % Enter the equation x'= - x + 5*sin(2*t) instead. % Click on "proceed" % % % % % % % % % % % % % % Click on "file --> print" to get in % the directional field into you local printer. % % % % % % % % % % % % % % % To get the corresponding vector field click on % "Options --Window settings", choose arrows and click on % "change settings". % Click on "print" to get a plot of the vector field. % % % % % % % % % % % % % % % % % Next click on % "Options --Keyboard input" and enter the initial data % t=0, x=1. Click on "compute" to get the solution curve % plotted on the directional field graph. % % Click on "print" to get the solution (UNIQUE!) to the % initial value % problem plotted on directional field graph. % % % % % % % % % % % % % % % Now you are going to plot your solution formula, % previously computed by yourself, and check that % coincides with the one of the Matlab dfield software: % go back to the window where the MATLAB prompt >> is, and type >> t=linspace(-2,10); (ENTER) % see that -2 and 10 are the bounds for t in the % dfield5 display window. % % next type your solution with the MATLAB grammar: >>y= sin(2*t)-2*cos(2*t) + 3*exp(-t); (ENTER) % next type >> plot(t,y,'*') (ENTER) % A new curve made of stars '*' is plotted on dfield % display window. % Compare both graphs. % If your solution is correct you should see (in principle) % NO DIFFERENCE with the one you obtained by % entering the initial data on dfield. % % % to label your graph use "title" command as follows >> title('dx/dt + x = 5sin(2t), x(0)=1, by M.E.') % Click on "print" to get your graph. % % % % % % % % % % % % % % % % % % % % % keep in mind that these are numerical computations, so eventually % THERE ARE DISCREPANCIES depending on the scale of your graph. % We shall address these issues later on. % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % You can also plot any function you wish, without using % dfield5. % For instance, just plot your computed solution function. % % First quit dfield by clicking "quit" on the display window. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >> t=linespace(-2,10); >> y= sin(2*t)-2*cos(2*t) + 3*exp(-t); % do not need to type the above sentences again, if you % already have used dfield to plot this function, % however repeating them, it does not matter. >> plot(t,y,'*') % or use plot(t,y) for a solid line curve. % % you'll see the graph of y(t) in a different scale % (chosen by matlab for this particular plot of this function) % In order to get the same scale as in the plot of dfield % you need to type % % % % % % % % % % % % % % % % % % >> axis([-2 10 -4 4]) %Change the y-axis to the values of the % dfield display. % label your graph >> title('Figure 1: y(t) = sin(2t)-2cos(2t) + 3exp(-t) by M.E.') % % label the axis >> xlabel('Independent Variable t') %label horizontal axis >> ylabel('Dependent variable y') %label vertical axis % % % and you may want to add a grid % >> grid % % % % % % % % % % % % % % % % % % % % % % % % % % %% % Print your plot by "file-->print" % % DO NOT FORGET!!! >> diary off >> quit %(to properly quit 'matlab') --------------------------------------------------------------------- Your matlab executions of this problem are simply: (see that I did not typed twice t=linspace(-2,10); and y= sin(2*t)-2*cos(2*t) + 3*exp(-t)) >> dfield % x'= -x + 5*sin(2*t) >> t=linspace(-2,10); >> y= sin(2*t)-2*cos(2*t) + 3*exp(-t); >> plot(t,y,'*') >> title('dx/dt + x = 5sin(2t), x(0)=1, by M.E.') >> plot(t,y,'*') >> axis([-2 10 -4 4]) >> title('Figure 1: y(t) = sin(2t)-2cos(2t) + 3exp(-t) by M.E.') >> xlabel('Independent Variable t') >> ylabel('Dependent variable y') >> grid >> diary off