DMelt:JMathlab/Plotting

From HandWiki
Member

Printf output

When you are working in the prompt, the answers are given automatically. If you have chosen to print output by executing a macro file (with the extension "m"), use the "printf()" method as in this example:

x=log(sqrt(854));        % natural logarithm
printf('Answer=%f\n', x);

You can output any symbolic variable as well. Here is a more complicated example where we print a value, symbolic equation and a matrix:

syms x,y;
a=2*sqrt(x)*exp(-x^2);
ff=subst(3,x,a);
printf("%f",ff);

ss=trigrat(sqrt(4*y^2+4*x*y-4*y+x^2-2*x+1));
printf("%f\n",ss);

M=[1:3 ; 4:6 ; 7:9];
C=M<4;
printf("%f\n",C);


Plotting

Data may be graphed using the plot(x,y)-function, x and y being equalsized vectors, which denote the coordinates of the data points to be plotted. A third optional argument plot(x,y,option) specifies plot options like colors and symbols, see exercise 7ff. The graphic may then be decorated (axis, title) and exported as encapsulated-postscript file for inclusion in text documents. With hold (or hold on) the graphic gets locked so that subsequent plot commands use the same window. Repeating hold (or hold off) deletes the graphic.


The method

plot(x,y [,option])

plots x versus y using option x and y are equalsized vectors. option is a string, specifying color (one of r,g,b,y,m,c,w,k) and symbol (one of +,*,o,x). Default: blue lines.

Logarithmic and semilogarithmic plots are provided with the functions loglog, linlog and loglin.


Here are the major plot methods:

Name(Arguments) Function
plot(x,y [,option]) Plot x versus y
loglog(x,y[,option]) logarithmic plot
linlog(x,y[,option]) semilogarithmic plot
loglin(x,y[,option]) semilogarithmic plot
print(name ) write graphic in eps-file


Example 1 let us plot the function: [math]\displaystyle{ y=\frac{1}{1+2x^2} }[/math] in the range x=0.01...100 linear and logarithmic. The code is below:

>> x=0.01:0.01:100; y=1./(1+0.5*x.*x); plot(x,y)
>> x=0.01:0.01:100; y=1./(1+0.5*x.*x); loglog(x,y)

Example 1a let us plot the function cos(x):

>> x=-10:0.1:10;
>> plot(x,cos(x));

Example 2: Display of Lissajous-figures: From the vector t=0:0.1:4*pi; create the trigonometric expressions x=sin(0.5*t+1); and y=cos(1.5*t);. The plot x vs. y is called Lissajous-figure. Create different figures by variating the constants 0.5,1,1.5 in the definition. Partial solution:

>> t=0:0.1:4*pi;
>> x=sin(0.5*t+1);
>> y=cos(1.5*t);
>> plot(x,y)

Example 3 Calculate the first 100 elements of the sequences

[math]\displaystyle{ x_n = \frac{n-1}{n}; x_n=\frac{n+1}{n}; x_n=\frac{n+(-1)^n}{n} }[/math]

Plot $x_n$ versus $n$ using the command plot(n, xn). Variate the plotoptions (colors, symbols).

>> n=1:100;
>> x1=(n-1)./n; x2=(n+1)./n; x3=(n+(-1).^n)./n;
>> plot(n,x1)        % Standard: blue, lines
>> hold
Current plot held.
>> plot(n,x2,'r')    % Color:  r,g,b,c,m,y,w.  
>> plot(n,x3,'g')

Example 4 Plot the datapoints of the following table using plot and colored symbols. Calculate the linear regression using polyfit, and plot the regression line in the same graph. Add title and labels, and export the graphic to a file suitable for inclusion in a text document.

x  0 	  1    2    3 	  4    5     6     7 	8 	9
y -3.1 -0.7  1.8  4.1 	6.2  8.9  11.3 	13.5 	16 	18.3
>> x=0:9;
>> y=[-3.1,-0.7,1.8,4.1,6.2,8.9,11.3,13.5,16,18.3];
>> plot(x,y,"+r")    % Symbol: o,x,+,*
>> hold
Current plot held.
>> plot(x,polyval(polyfit(x,y,1),x)) % Regression
>> xlabel("x-Achse")
>> ylabel("y-Achse")
>> title("Beispielgraph")
>> print("graph.eps") % not Applet or Midlet!