w04 d01 lab computational methods

Upload: kenn-senados

Post on 03-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 W04 D01 LAB Computational Methods

    1/20

    W04_D01

    Computational Methods in Physics

    PHYS168

  • 7/27/2019 W04 D01 LAB Computational Methods

    2/20

    Plot in FreeMat

    Graph 2-D data with linear scales for both axes

    Basic Command Syntax:

    plot(,{linespec 1},,{linespec 2}...,properties...)

    **In general, the linespec is composed of three optional parts, the colorspec,the symbolspecand the linestylespecin any order. Each of thesespecifications is a single character that determines the correspondingcharacteristic.

    Example:

    X = linspace(-pi, pi);

    Y = cos(X);

    Plot (X, Y, r*-)

  • 7/27/2019 W04 D01 LAB Computational Methods

    3/20

    Plot in FreeMat

    Example:

    X = linspace(-pi, pi);

    Y = cos(X);

    Plot (X, Y, r*-)

  • 7/27/2019 W04 D01 LAB Computational Methods

    4/20

    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

  • 7/27/2019 W04 D01 LAB Computational Methods

    5/20

    Plot in FreeMat

    The symbolspecspecifies the (optional) symbol to bedrawn 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

    '' - Right-pointing triangle symbol

  • 7/27/2019 W04 D01 LAB Computational Methods

    6/20

    Plot in FreeMat

    The linestylespecspecifies 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

  • 7/27/2019 W04 D01 LAB Computational Methods

    7/20

    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)

  • 7/27/2019 W04 D01 LAB Computational Methods

    8/20

    Plot in FreeMat

    Example: SUPERIMPOSE MULTIPLE GRAPHS IN ONE CANVASx = 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*:');

  • 7/27/2019 W04 D01 LAB Computational Methods

    9/20

    Plot in FreeMat

    Example: for complex matrices y = cos(2*x) + i * cos(3*x);

    plot(y);

    plot(real(y))

    plot(imag(y))

  • 7/27/2019 W04 D01 LAB Computational Methods

    10/20

    Plot in FreeMat

    Example: for complex matrices t = linspace(-3,3);

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

  • 7/27/2019 W04 D01 LAB Computational Methods

    11/20

    Plot in FreeMat

    Other tweaks in plotting graphs

    Title

    title( title here )

    Axes labels axes Range

    xlabel( put x-axis label here ) axis([ 0, 10, -1, 1])

    ylabel( put y-axis label here )

    Legends

    legend(legend 1,legend 2,)

  • 7/27/2019 W04 D01 LAB Computational Methods

    12/20

    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(:) ) );

  • 7/27/2019 W04 D01 LAB Computational Methods

    13/20

    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')

  • 7/27/2019 W04 D01 LAB Computational Methods

    14/20

    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)

  • 7/27/2019 W04 D01 LAB Computational Methods

    15/20

    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.

  • 7/27/2019 W04 D01 LAB Computational Methods

    16/20

  • 7/27/2019 W04 D01 LAB Computational Methods

    17/20

  • 7/27/2019 W04 D01 LAB Computational Methods

    18/20

  • 7/27/2019 W04 D01 LAB Computational Methods

    19/20

    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 matcheswith the natural frequency of the physical

    system. If the driving force is periodic, the

    oscillation is expressed as dierential equation,

  • 7/27/2019 W04 D01 LAB Computational Methods

    20/20

    Problem 1, contd where m is the mass, k is the spring constant, b is the

    damping parameter, Fe is the amplitude of the drivingforce and we is the frequency of the periodic drivingforce. The solution to this

    dierential equation yields,

    where u = we

    /w and w is the natural frequency of thephysical system.Obtain plots of the amplitude (Fe/G) of Equation 1.6versus we=w for b = 0.100 , b = 0.200 and b =0.400 . Use the following values: F

    e

    = 10, m = 1 and k= 1