You are on page 1of 20

W04_D01 Computational Methods in Physics

PHYS168

Plot in FreeMat
Graph 2-D data with linear scales for both axes Basic Command Syntax:
plot(<data 1>,{linespec 1},<data 2>,{linespec 2}...,properties...) **In general, the linespec is composed of three optional parts, the colorspec, the symbolspec and the linestylespec in any order. Each of these specifications is a single character that determines the corresponding characteristic. Example:

X = linspace(-pi, pi); Y = cos(X); Plot (X, Y, r*-)

Plot in FreeMat
Example:

X = linspace(-pi, pi); Y = cos(X); Plot (X, Y, r*-)

Plot in FreeMat
colorspec:
'b' - Color Blue 'g' - Color Green 'r' - Color Red 'c' - Color Cyan 'm' - Color Magenta 'y' - Color Yellow 'k' - Color Black

Plot in FreeMat
The symbolspec specifies the (optional) symbol to be drawn at each data point:
'.' - Dot symbol 'o' - Circle symbol 'x' - Times symbol '+' - Plus symbol '*' - Asterisk symbol 's' - Square symbol 'd' - Diamond symbol 'v' - Downward-pointing triangle symbol '^' - Upward-pointing triangle symbol '<' - Left-pointing triangle symbol '>' - Right-pointing triangle symbol

Plot in FreeMat
The linestylespec specifies the (optional) line style to use for each data series:
'-' - Solid line style ':' - Dotted line style '-.' - Dot-Dash-Dot-Dash line style '--' - Dashed line style

Plot in FreeMat
Example: SUPERIMPOSE MULTIPLE GRAPHS IN ONE CANVAS
x = linspace(-pi,pi); y = [cos(x(:)), cos(3*x(:)), cos(5*x(:))];

plot(x,y)

Plot in FreeMat
Example: SUPERIMPOSE MULTIPLE GRAPHS IN ONE CANVAS
x = linspace(-pi,pi); y = [cos(x(:)), cos(3*x(:)), cos(5*x(:))];

plot(x, y(:,1), 'rx-', x, y(:,2), 'b>-', x, y(:,3), 'g*:');

Plot in FreeMat
Example: for

complex matrices

y = cos(2*x) + i * cos(3*x); plot(y); plot(real(y)) plot(imag(y))

Plot in FreeMat
Example: for

complex matrices

t = linspace(-3,3); plot(cos(5*t).*exp(-t),'r-','linewidth',3);

Plot in FreeMat
Other tweaks in plotting graphs Title title( title here ) Axes labels xlabel( put x-axis label here ) ylabel( put y-axis label here ) Legends legend(legend 1, legend 2, ) axes Range axis([ 0, 10, -1, 1])

Plot in FreeMat
Multiple graphs in a canvas (NOT Superimposed) x = linspace(-pi, pi); subplot(2,2, 1); plot( x, cos( x ) ); subplot(2,2, 2) plot( x, cos(2*x(:) ) ); subplot(2,2, 3); plot( x, cos(5*x(:) ) ); subplot(2,2, 4); plot( x, cos(10*x(:) ) );

Saving Variables in a File in FreeMat


Saving matrices or variables into an external file Syntax: save(filename, variable1, variable2,, options) Example: THIS IS IN BINARY FORMAT

savefile = 'pqfile.mat'; p = rand(1, 10); q = ones(10); save(savefile, 'p', 'q')

Loading Variable from a File in FreeMat


Loading matrices or variables from an external file Syntax: load(filename) load filename Example: savefile = 'pqfile.mat'; p = rand(1, 10); q = ones(10); save(savefile, 'p', 'q') To load the variables p and q: load pqfile.mat load pqfile.txt After that we can then access the variables directly; example disp(p) or disp(q)

Problem: Underdamped LRC circuit


% Consider the LRC circuit as shown we want to analyze this LRC circuit's % behavior using Kirchhoff's Rules. First we close the switch S in the % upward position connecting the capacitor to an emf (E) source for a long time % to ensure that the capacitor acquires its final charge Q=EC. Then at t=0, % we turn the switch on, applying Kirchhoff's Loop Rule, with clockwise as the % direction of travel, we can obtain : % sum( V ) = -iR -L (di/dt) - q/C = 0 [1] % where i is the current flowing in the circuit at time t, q is the accumulated % charge in the capacitor at a certain time, R i the resistance of the resistor % L is the inductance of the inductor and C is the capacitance of the capacitor. % Substituting i = dq/dt, abouve eqn [1] becomes % d^2q/dt^2 + (R/L)(dq/dt) + q/(LC) = 0 [2] % There are three possible solutions to eqn [2]. The first solution corresponds % to the situation where (R/2L)^2 < (1/LC), also known as the underdamped case. % The two remaining cases are the overdamped case, where (R/2L)^2 > (1/LC), % and the critical damped case, where (R/2L)^2 = (1/LC). % OUR interest here is on the underdamped case, where the solution to eqn [2] % is actually a clear combination of exponential decay and sinusoidal function, % as can be seen in the eqn [3] below % q(t) = ( A exp[(-R/2L)t] ) cos( [sqrt( 1/LC - (R/2L)^2 ) * t + phi ] ) [3] % note charge is a function of time, let time = 0 10 TASK: plot the sinusoidal exponential decay and exponential decay-only cases.

Problem 1
Consider the resonant behavior of a forced oscillation. As discussed in many introductory physics textbooks, resonance occurs whenever the frequency of the driving force matches with the natural frequency of the physical system. If the driving force is periodic, the oscillation is expressed as dierential equation,

where m is the mass, k is the spring constant, b is the damping parameter, Fe is the amplitude of the driving force and we is the frequency of the periodic driving force. The solution to this

Problem 1, contd

dierential equation yields,


where u = we/w and w is the natural frequency of the physical system. Obtain plots of the amplitude (Fe/G) of Equation 1.6 versus we=w for b = 0.100 , b = 0.200 and b = 0.400 . Use the following values: Fe = 10, m = 1 and k = 1.

You might also like