gdangelmayr.us

Introduction to Ordinary Differential Equations
Course Description Notes and Files

1) Plotting functions using fplot:

a) Basic

b) Plotting several graphs

c) Plotting inline-functions

2) Saving Plots

 

1) Plotting functions using fplot:

a) Basic: the fplot-command can be applied to all standard functions such as sin(x), cos(x), exp(x), log(x) (natural log) arctan(x) etc. For example, executing

fplot('sin(5*x)',[0 2])

in the command window or from a script file, plots sin(5*x) in the interval 0 £ x £ 2.

You can combine functions to any analytical expression. For example, if you want to plot sin(5*x)*cos(0.3*x)+exp(-2*x) with labels attached to the axes, type

fplot('sin(5*x)*cos(0.3*x)+exp(-2*x)',[0 2]),xlabel('x'),ylabel('y')

(top)

b) Plotting several graphs: To plot several graphs in one figure, you have to use the command hold on to tell Matlab that previous plots should be preserved.

Example: To plot y(t)=c*exp(-t^2/2) in the interval -2£ x £ 2 for c=1,2,3 in the same figure, execute


fplot('exp(-t^2)',[-2 2])
hold on
fplot('2*exp(-t^2)',[-2 2])
fplot('3*exp(-t^2)',[-2 2])
hold off

(top)

c) Plotting inline-functions: The fplot-command can also be applied to user-defined functions. There are two types of functions a user can define: inline-functions and function files. Here we consider only inline functions.

Example: The command

myexp2=inline('2*exp(-t^2/2)')

generates your own function myexp that depends on a single variable (t). You can plot this function using fplot:

fplot(myexp2,[0 2])

Note: function-name of an inline-function is not quoted!!

The advantage of using inline functions is that you can define functions of several variables. If you do this, you should declare the order in which the variables occur as inputs.

Example: The command

myexp_c=inline('c*exp(-t^2/2)','t','c')

creates a function that depends on two variables (t,c). You can numerically evaluate this function, for example at (t,c)=(1,1), by typing

myexp_c(1,2)

To plot this function as function of t for the parameter value c=3, type

fplot(myexp_c,[-2 2],[],[],[],3)

The three brackets are placeholders for three options you can adjust (which we don't describe here - type help fplot if you want to know the details).

Using inline-functions has the advantage that you can easily cause Matlab to plot an arbitrary number of graphs in one plot. For example, if you want to plot the function y(t)=c*exp(-t^2/2) for c=-10,-9,..,9,10, type

for c=-10:10
    fplot(myexp_c,[-2 2],[],[],[],c)
    xlabel('t'),ylabel('y')
    hold on
end
hold off

(If you want to plot the function for the parameter values c=-1.2,-1,...,0.6,0.8 instead, replace c=-10:10 by c=-1.2:0.2:0.8.)

Finally assume you want to plot the function for the six c-values c=-0.6, -0.35, -0.1, 0.12, 0.275, 0.4. To accomplish this, execute

c=[-0.6 -0.35 -0.1 0.12 0.275 0.4];
for n=1:6
    cv=c(n)
    fplot(myexp_c,[-2 2],[],[],[],cv)
    xlabel('t'),ylabel('y')
    hold on
end
hold off

(top)

2) Saving Plots

a) To save the active figure window in Matlab format (extension: .fig) click file in the figure menu and then Save As.

b) To save the plot in jpg-format (with quality level of 100) , type

print -djpeg100 filename

c) Same as b) in bmft-format:

print -dbitmap filename

For further details type help print.

To avoid waste of paper, it is strongly recommended that in your homework Matlab commands and figures are inserted, for example, in a word document and kept in a reasonable size.

(top)