5 functions and files.pdf

Upload: augusto-de-la-cruz-camayo

Post on 02-Jun-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 5 Functions and Files.pdf

    1/35

    MATLABFunctions and Files

    Cheng-Liang ChenPSELABORATORY

    Department of Chemical EngineeringNational TAIWAN University

  • 8/11/2019 5 Functions and Files.pdf

    2/35

    Chen CL 1

    Using Files filename1.m

    Used for MATLAB programs and for some MATLAB functions M-files are ASCII (American Standard Code for Information Interchange)

    files that are written in the MATLAB language(Can be created using any word processor or text editor)

    filename2.mat

    Used to save the names and values of variables created during aMATLAB session

    MAT-files arebinaryfiles (more compact storage thanASCIIfiles) readable only by the software that created them

    ASCII Data Files Header: comments describing what the data represents, the date it was

    created, who created the data . . . One or more lines of data

  • 8/11/2019 5 Functions and Files.pdf

    3/35

    Chen CL 2

    Controlling Input and Output

    speed = 63;disp(The predicted speed is:)

    disp(speed)

    The predicted speed is:

    63

  • 8/11/2019 5 Functions and Files.pdf

    4/35

    Chen CL 3

    Input/Output Commands

    Command Description

    disp(A) Displays the contents, but not the name, of thearray A.

    disp(text) Displays thetext stringenclosed within single quotes.

    format Controls the screens output display format (Table3.3-2).

    fprintf Performs formatted writes to the screen or to a file (seeAppendix C).

    x=input(text) Displays thetextin quotes, waits for user input from thekeyboard, and stores thevalue in x.

    x=input(text,s) Displays thetextin quotes, waits for user input from thekeyboard, and stores the input as a string in x.

    k=menu(title,option1,option2,...)

    Displays a menu whose title is in the string variable title,and whose choices are option1,option2,. . .

  • 8/11/2019 5 Functions and Files.pdf

    5/35

    Chen CL 4

    Ex: Design of A Parallel-plate CapacitorCapacitors are widely used in electric circuits. A capacitor stores energy bymaintaining the charge separation produced when a voltage is applied to thedevice. A parallel-plate capacitor is constructed from two or more parallelconducting plate, each having anarea A and separated from each other by air oranother dielectric material such as mica or paper. If the plates are separated by adistanced, the devicescapacitance C, which is a measure of its energy storagecapacity, can be computed from the formula

    C= (n 1) Ad

    where n is the number of platesand is the permittivity of the dielectric material.

    The unit of capacitance is thecoulomb/voltand is called thefarad(F). Suppose

    we use plates having an area A= 20 centimeters2, separated by a distance d= 4

    millimeters with air, for which= 8.85 1012 farad/meter. Construct a table toselect the number of plates needed to obtain a desired capacitance value. Assume

    that no more than 10 plates will be used.

    C C

  • 8/11/2019 5 Functions and Files.pdf

    6/35

    Chen CL 5

    Ex: Design of A Parallel-plate Capacitor% Program capacitor.m

    % Generates a table of capacitance values.

    %

    % Define the values of the constants.

    permittivity = 8.85e-12;

    A = 20/100^2; % Convert A to square meters

    d = 4/1000; % Convert d to meters

    %

    n = [2:10]; % Vector n is # of plates% Generates capacitance values

    % (Multiply by 10^12 for

    % display purposes)

    C = ((n-1)*permittivity*A/d)*1e12;

    %

    table(:,1) = n; % Create first columntable(:,2) = C; % Create second column

    % Display table heading

    % and table itself.

    disp(No. Plates Capa.(F)xe12)

    disp(table)

    No. Plates Capa.(F)xe12

    2.0000 4.4250

    3.0000 8.8500

    4.0000 13.27505.0000 17.7000

    6.0000 22.1250

    7.0000 26.5500

    8.0000 30.9750

    9.0000 35.4000

    10.0000 39.8250

    Ch CL 6

  • 8/11/2019 5 Functions and Files.pdf

    7/35

    Chen CL 6

    Numeric Display Formats

    Command Description and Example

    format short Four decimal digits (the default);13.6745

    format long 16 digits; 17.27484029463547

    format short e Five digits (four decimals) plus exponent;6.3792e+03

    format long e 16digits (15decimals) plus exponent;6.379243784781294e-04format bank Two decimal digits; 126.73

    format + Positive, negative, or zero; +

    format rat Rational approximation;43/7

    format compact Suppresses some line feeds

    format loose Resets to less compact display mode

    Ch CL 7

  • 8/11/2019 5 Functions and Files.pdf

    8/35

    Chen CL 7

    User Input

    TheInput function displays text on the screen, waits for the user

    to enter something from the keyboard, and then stores the inputin the specified variable

    x = input(Please enter the value of x: )

    Calender = input(Enter the day of the week: , s)

    k = menu(title, option1, option2,...)

    Demo the following in the class:

    k = menu(Choose a data marker, o, *,x);type = [o, *,x];

    x = [1,2,3,4]; y = [2,4,3,5];

    plot(x,y, x,y, type(k),LineWidth,2,MarkerSize,3)

    set(gca,LineWidth,2,FontSize,16)

    title(Test the use of menu,FontSize,16)

    Ch CL 8

  • 8/11/2019 5 Functions and Files.pdf

    9/35

    Chen CL 8

    Test Your Understanding

    T3.0-1 Write a script file to compute and display a table toconvert from radians to degrees. Use five values for the radian

    angle: 1, 2, 3, 4,and5radians. Be sure to label each column in the

    table.

    T3.0-2 The volume V of a sphere depends on its radius r asfollows: V = 4r3/3. Write a script file to compute and display

    a table showing the volume in cubic meters versus the radius in

    meters, for1 r 2, in increments of0.1. Label each column.

    T3.0-3 The surface area A of a sphere depends on its radius r as

    follows: A = 4r2. Write a script file that prompts the user to

    enter a radius, computes the surface area, and displays the result.

    Ch CL 9

  • 8/11/2019 5 Functions and Files.pdf

    10/35

    Chen CL 9

    Some Common Mathematical FunctionsExponential

    exp(x) Exponential; ex

    sqrt(x) Square root;xLogarithmic

    log(x) Natural logarithm;ln(x)

    log10(x) Common (base10) logarithm; log(x) = log10(x)

    Complex

    abs(x) Absolute value;xangle(x) Angle of a complex numberx

    conj(x) Complex conjugate

    imag(x) Imaginary part of a complex numberx

    real(x) Real part of a complex number x

    Numericceil(x) Round to the nearest integer towardfix(x) Round to the nearest integer toward zero

    round(x) Round toward the nearest integer

    sign(x) Sigmum function: +1 ifx >0; 0 ifx= 0;1 ifx

  • 8/11/2019 5 Functions and Files.pdf

    11/35

    Chen CL 10

    Complex Number Functions

    x = - 3 + 4 i ;

    y = 6 - 8 i ;mag_x = abs(x)

    mag_x =

    5.0000

    mag_y = abs(y)

    mag_y =

    10.0000

    mag_product = abs(x*y)50.0000

    angle_x = angle(x)

    angle_x =

    2.2143

    angle_y = angle(y)

    angle_y =

    -0.9273

    sum_angles = angle_x + an

    sum_angles =

    1.2870

    angle_product = angle(x*y

    angle_product =

    1.2870

    Chen CL 11

  • 8/11/2019 5 Functions and Files.pdf

    12/35

    Chen CL 11

    Test Your Understanding

    T3.1-1For several values ofx and y, confirm that ln(xy) = ln x+ ln y.

    T3.1-2

    Find the magnitude, angle, real part, and imaginary part of the

    number 2 + 6i.

    Chen CL 12

  • 8/11/2019 5 Functions and Files.pdf

    13/35

    Chen CL 12

    Trigonometric Functions

    Trigonometric

    cos(x) Cosine; cos x

    cot(x) Cotangent; cot x

    csc(x) Cosecant; csc x

    sec(x) Secant; sec x

    sin(x) Sine; sin x

    tan(x) Tangent; tan xInverse trigonometric

    acos(x) Inverse cosine;cos1x

    acot(x) Inverse cotangent;cot1x

    acsc(x) Inverse cosecant;csc1x

    asec(x) Inverse secant; sec1x

    asin(x) Inverse sine;sin1x

    atan(x) Inverse tangent;tan1x

    atan2(y,x) Four-quadrant inverse tangent

    Chen CL 13

  • 8/11/2019 5 Functions and Files.pdf

    14/35

    Chen CL 13

    Test Your Understanding

    T3.1-3For several values ofx, confirm that eix = cos x+i sin x.

    T3.1-4

    For several values of x in the range 0

    x

    2, confirm that

    sin1 x+ cos1 x= /2.

    T3.1-5

    For several values of x in the range 0 x 2, confirm thattan(2x) = 2 tan x/(1 tan

    2

    x).

    Chen CL 14

  • 8/11/2019 5 Functions and Files.pdf

    15/35

    Chen CL 14

    Hyperbolic functions

    Hyperbolic

    cosh(x) Hyperbolic Cosine; cosh x= (ex +ex)/2

    coth(x) Hyperbolic Cotangent;cosh x/ sinh x

    csch(x) Hyperbolic Cosecant;1/ sinh x

    sech(x) Hyperbolic Secant;1/ cosh x

    sinh(x) Hyperbolic Sine;sinh x= (e

    x

    ex

    )/2tanh(x) Hyperbolic Tangent;sinh x/ cosh x

    Chen CL 15

  • 8/11/2019 5 Functions and Files.pdf

    16/35

    Chen CL 15

    Inverse Hyperbolic functions

    Inverse hyperbolicacosh(x) Inverse hyperbolic cosine;

    cosh1 x= ln(x+

    x2 1), x 1acoth(x) Inverse hyperbolic cotangent;

    coth1 x= 12

    ln(x+1x1

    ), x >1 orx