mat lab intro elmer

59
CEE15 Introduction to Computing for Civil Engineers An Introduction to MATLAB (Version 4.61 Release 1;) Chapter 1 Introduction to MATLAB 1.1. MATLAB Windows Table 1.1 MATLAB Windows Window Purpose "o$$a&’ (i&’o* +ai& *i&’o*, e&ters 1aria2les, ru&s 5ro6ra$s 7i6ure (i&’o* "o&tai&s out5ut fro$ 6ra59ic co$$a&’s E’itor (i&’o* "reates a&’ ’e2u6s scri5t a&’ fu&ctio& files <el5 (i&’o* Pro1i’es 9el5 i&for$atio& >au&c9 Pa’ (i&’o* Pro1i’es access to tools, ’e$os, a&’ ’ocu$e&tatio& "o$$a&’ <istor? (i&’o* >o6s co$$a&’s e&tere’ i& t9e "o$$a&’ (i&’o* (or@s5ace (i&’o* Pro1i’es i&for$atio& a2out t9e 1aria2les t9at are use’ "urre&t Airector? (i&’o* B9o*s t9e files i& t9e curre&t ’irector? 1.2. MATLAB Commands MATLAB: +atriD >a2orator?E T9e 2asic ele$e&t of +GT>GH is t9e $atriDE +GT>GH co$$a&’s are si$ilar to t9e *a? *e eD5ress e&6i&eeri&6 ste5s i& $at9e$atics, *riti&6 co$5uter solutio&s i& +GT>GH is $uc9 Iuic@er t9a& *riti&6 co$5uter solutio&s usi&6 a lo*Jle1el la&6ua6e suc9 as " or 7ortra&E ! Belect t9e +GT>GH 5ro6ra$ fro$ a $e&u i& ?our o5erati&6 s?ste$ K(i&’o*s or LMNOPE +GT>GH co$$a&’ *i&’o* *ill a55ear a&’ ?ou s9oul’ see t9e +GT>GH 5ro$5t K>>P, *9ic9 tells ?ou t9at +GT>GH is *aiti&6 for ?ou to e&ter a co$$a&’E ! T9e co$$a&’ to eDit +GT>GH is quit or exitE ! Nf ?ou *a&t to sa1e all t9e 1aria2les i& t9e *or@s5ace 2efore ?ou Iuit, use t9e co$$a&’ saveE T9is auto$aticall? sa1es all t9e 1aria2les i& a file calle’ matlab.matE ! Gfter Iuitti&6 +GT>GH if ?ou reJe&ter o&ce a6ai& a&’ if ?ou *a&t to use all t9e 1aria2les i& t9e *or@s5ace t9e& use t9e co$$a&’ load, t9e ’efault file &a$e is matlab.mat. ! T9e co$$a&’s clear all or clear re$o1es all t9e 1aria2les fro$ t9e $e$or?E ! clc # To clear t9e co$$a&’ *i&’o*E ! clg # To clear t9e 6ra59ic *i&’o*E ! clf # To clear t9e curre&t fi6ure aE@Ea clear t9e 6ra59 *i&’o*E ! Ctrl C is use’ to 6e&erate a local a2ort *it9i& +GT>GH Ki& case of i&fi&ite loo5PE ! help # a 9el5 $e&u a55earsE ! To 6et 9el5 o& a 5articular to5ic, use help to5ic 7or eDa$5le, help dispE ! +GT>GH is a caseJse&siti1e la&6ua6e, for eDa$5le, TIME a&’ time are t*o ’iffere&t 1aria2lesE ! T9e co$$a&’ who lists t9e 1aria2les t9at ?ou 9a1e ’efi&e’E ! T9e co$$a&’ whos lists t9e 1aria2les alo&6 *it9 t9eir siQes a&’ *9et9er t9e? 9a1e &o&Qero i$a6i&ar? 5artE R

Upload: naresh-kumar

Post on 24-Nov-2015

51 views

Category:

Documents


1 download

DESCRIPTION

MatLab

TRANSCRIPT

  • CEE15 Introduction to Computing for Civil Engineers !

    An Introduction to MATLAB (Version 4.61 Release 1;)

    Chapter 1 Introduction to MATLAB

    1.1. MATLAB Windows Table 1.1 MATLAB Windows

    Window Purpose "o$$a&'!(i&'o*! +ai&!*i&'o*,!e&ters!1aria2les,!ru&s!5ro6ra$s!7i6ure!(i&'o*!! "o&tai&s!out5ut!fro$!6ra59ic!co$$a&'s!E'itor!(i&'o*! "reates!a&'!'e2u6s!scri5t!a&'!fu&ctio&!files!au&c9!Pa'!(i&'o*! Pro1i'es!access!to!tools,!'e$os,!a&'!'ocu$e&tatio&!"o$$a&'!GH!Ki&!case!of!i&fi&ite!loo5PE !" help #!a!9el5!$e&u!a55earsE !" To!6et!9el5!o&!a!5articular!to5ic,!use!help to5ic!

    7or!eDa$5le,!help dispE!!" +GT>GH! is! a! caseJse&siti1e! la&6ua6e,! for! eDa$5le,! TIME a&'! time are! t*o! 'iffere&t!

    1aria2lesE!!!" T9e!co$$a&'!who lists!t9e!1aria2les!t9at!?ou!9a1e!'efi&e'E!!" T9e!co$$a&'!whos lists! t9e!1aria2les!alo&6!*it9! t9eir! siQes!a&'!*9et9er! t9e?!9a1e!&o&Qero!

    i$a6i&ar?!5artE!

    ! R

  • CEE15 Introduction to Computing for Civil Engineers !

    !" Glso!who a&'!whos *ill!list!a!1aria2le!&a$e'!ans Ki&!co$$a&'!*i&'o*PE!Ans!stores!t9e!1alue!of!t9e!eD5ressio&!t9at!*as!last!co$5ute'!2ut!&ot!assi6&e'!!a!&a$eE!7or!eDa$5le,!

    ! !!!!!a=[2 3]; b=[3 5]; a+b; a.*b; who;

    T9e!a2o1e!5ro6ra$!*ill!9a1e!a&!out5ut!as!t9e!follo*i&6!Kif!?ou!t?5e!t9e$!i&!co$$a&'!*i&'o*P:!Sour!1aria2les!are:!

    ! a!!!!!a&s!!!!!2!*9ere!a&s!stores!t9e!1alue!of!aET2E!!!" M-files!K!TestRE$,!TestUE$,VetcP!are!use'!to!eDecute!seIue&ce!of!co$$a&'s!t9at!are!store'!i&!

    t9e!file!i&!a''itio&!to!eDecuti&6!co$$a&'s!e&tere'!t9rou69!t9e!@e?!2oar'E!M-file!Kalso!@&o*&!as! t9e! script fileP! is! a! file! of! GB"NN! c9aracters! a&'! is! 6e&erate'! usi&6! a&! e'itor! or! *or'!5rocessorE!To!eDecute!t9e!co$$a&'s!i&!t9e!M-file!sa?!Test1.m,!*e!&ee'!to!e&ter!t9e!co$$a&'!Test1 i&!t9e!co$$a&'!*i&'o*!or!ru&!t9e!M-file fro$!t9e!$e&uE!Sou!ca&!create!a!&e*!M-file or!e'it!a&!eDisti&6!M-file fro$!t9e!$e&uE!Glso!for!t9e!eDisti&6!file,!?ou!ca&!use!t9e!co$$a&'!edit to!e'it!t9at!fileE!

    !" % i&'icates!co$$e&t!li&e!i&!t9e!5ro6ra$E!!" (ellipsisP!i&'icates!t9at!t9e!ro*!is!to!2e!co&ti&ue'E!!" T9e!co$$a&'!echo Ki&!t9e!co$$a&'!*i&'o*P!causes M-files to!2e!1ie*e'!as!t9e?!eDecuteE!

    echo on!tur&s!o&!ec9oi&6!of!co$$a&'s!i&si'e!Bcri5tJfilesE!!!!!!! echo off!tur&s!off!ec9oi&6E! Ec9oi&6!ca&!2e!tur&e'!o&!a&'!off!*it9!t9e!co$$a&'!echoE!!!!!! a=[2,3]; b=[3 5]; c=[1:4;10:13]; a+b; a.*b; disp(a) disp(ans) disp(c) !T9e!a2o1e!M-file *ill!6i1e!us!a&!out5ut!*it9out!t9e!co$$a&'!echo as,!! U!!!!!W!!!!!!X!!!!RY!!!!!!R!!!!!U!!!!!W!!!!!Z!!!!!R[!!!!RR!!!RU!!!RW!!a&'!a&!out5ut!*it9!t9e!co$$a&'!echo as,!a\]U,W^_!2\]W!Y^_!c\]R:Z_R[:RW^_!a`2_!aET2_!'is5KaP!!!!!!U!!!!!W!'is5Ka&sP!!!!!!X!!!!RY!

    ! U

  • CEE15 Introduction to Computing for Civil Engineers !

    'is5KcP!!!!!!R!!!!!U!!!!!W!!!!!Z!!!!!R[!!!!RR!!!!RU!!!!RW!!!" T9e!co$$a&'!what is!t9e!'irector?!listi&6s!of!all!t9e!M-files!a&'!mat-files.!!" T9e!co$$a&'!type Test1.m or type Test1!lists!t9e!co&te&ts!of!a!s5ecifie'!file!&a$eE !" To!9alt!a!5ro6ra$!te$5oraril?,!*e!ca&!use!t9e!co$$a&'!pauseE!T9e!co$$a&'!pause (30) *ill!

    9alt!t9e!5ro6ra$!for!W[!seco&'sE!To!co&ti&ue!*it9!our!5ro6ra$!*e!&ee'!to!9it!t9e!enter @e?!o&!our!@e?2oar'E!!

    % This program reads the values of the matrices a and b % and computes and displays their addition and subtraction. a = input('Enter the value of a'); b = input('Enter the value of b'); pause c = a + b; d = a - b; disp('c=') disp(c) pause(5) disp('d=') disp(d)

    !" +atla2!*ill!acce5t!$a&?!of!t9e!sa$e!co$$a&'s!as!u&iDE!!7or!eDa$5le,!ls!*ill!list!t9e!curre&t!'irector?,!c'!*ill!c9a&6e!itE!!5*'!6i1es!?ou!t9e!5rese&t!*or@i&6!'irector?E!!!

    !" T9e!curre&t!'irector?!is!also!liste'!i&!t9e!tool2ar!a2o1e!t9e!co$$a&'!*i&'o*E!!!!" T?5i&6!path!*ill!6i1e!?ou!$atla2as!curre&t!searc9!5at9E!!T9is!is!*9ere!it!loo@s!for!co$$a&'s!

    a&'!scri5ts!to!ru&!*9e&!?ou!t?5e!so$et9i&6!i&to!t9e!co$$a&'!*i&'o*E!!! 1.3. Basic Built In Functions in MALAB!1.3.1. Elementary Math Functions

    Table 1.2 Math functions in MATLAB Function Description a2sKP! G2solute!1alue!eD5KP! ED5o&e&tial!

    factorialKP! T9e!factorial!fu&ctio&!lo6KP! Matural!lo6arit9$!lo6R[KP! Hase!R[!lo6arit9$!sIrtKP! BIuare!root!

    1.3.2. Trigonometric Math Functions Table 1.3 Trigonometric functions in MATLAB

    Function Description Function Description acosKP! N&1erse!cosi&e! cosKP! "osi&e!acotKP! N&1erse!cota&6e&t! cotKP! "ota&6e&t!asi&KP! N&1erse!si&e! si&KP! Bi&e!ata&KP! N&1erse!ta&6e&t! ta&KP! Ta&6e&t!

    1.3.3. Hyperbolic Math Functions Table 1.4 Hyperbolic functions in MATLAB

    Function Description Function Description cos9KP!

  • CEE15 Introduction to Computing for Civil Engineers !

    !Note:!all!t9e!c9aracters!i&!fu&ctio&!&a$e!s9oul'!2e!i&!t9e!lo*er!caseE

    ! Z

  • CEE15 Introduction to Computing for Civil Engineers !

    Chapter 2 Scalar, Vector and Matrix Operations

    2.1. Scalars 2.1.1. Special Values pi: $!KUUbc!or!WERZRXP!i, j: R% !inf: &!KN&fi&it?,!for!eDa$5le,!it!*ill!2e!t9e!result!for!a&?!&u$2er!'i1i'e'!2?!QeroP!nan: Mot!a!&u$2er,!for!eDa$5le,!it!*ill!2e!t9e!result!for!t9e!'i1isio&!of!Qero!2?!Qero!clock: T9is!ele$e&t!retur&s!t9e!curre&t!ti$e!i&!a!siDJele$e&t!ro*!1ector!co&tai&i&6!?ear,!$o&t9,!'a?,!

    9our,!$i&ute!a&'!seco&'sE!date: T9is!fu&ctio&!retur&s!t9e!curre&t!'ate!i&!a!c9aracter!stri&6!for$at,!suc9!as!U[Jdu&JeUE!!

    !"

  • CEE15 Introduction to Computing for Civil Engineers !

    !Precedence in Arithmetic Operations: Precedence! ! Operation!

    R! Pare&t9esis!U! ED5o&e&tiatio&,!left!to!ri69t!W! +ulti5licatio&!a&'!'i1isio&,!left!to!ri69t!Z! G''itio&!a&'!su2tractio&,!left!to!ri69t!

    !Arithmetic operation between two scalars: Operation Algebraic form MATLAB expression G''itio&! ! ! a!`!2! ! ! a!`!2!Bu2tractio&! ! ! a!i!2! ! ! a!i!2!+ulti5licatio&!! ! a!'!2! ! ! a!T!2!

    Ri69t!'i1isio&!!!!!!!!!!!!!!! !!ba !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!a!b!2!

    ED5o&e&tiatio&! !!!! !! ! !!!!!!!!!!!!!!!!!!!!!!!!!a!h!2!ba

    >eft!'i1isio&!!!!!!!!!!!!!!!!! !!ab !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!a!g!2!

    !EDa$5le!R:!"o&si'er!t9e!follo*i&6!5ro6ra$:!a=2; b=3; x=a+b; fprintf('a=%1.0f\n',x) count=1; count=count+1; count time=0.0; time=5.0; disp('time') disp(time) !T9e!a2o1e!5ro6ra$!*ill!6i1e!us!t9e!follo*i&6!out5ut:!a=5 count = 2 time 5 EDa$5le!U:!"o&si'er!t9e!follo*i&6!5ro6ra$:!%area of a trapezium 1/2 * height * (base1 +base2) h=2; b1=3; b2=5; area=0.5*h*(b1+b2); disp('area') disp(area) !T9e!out5ut!*ill!2e:!area 8

    ! X

  • CEE15 Introduction to Computing for Civil Engineers !

    !" T9e!eD5ressio&!!!W U

    U

    U XEW[E[Y[[Y WERZ

    x x xx x

    % ( %( %

    ca&!2e!*ritte&!i&!+GT>GH!as,!

    !KDhWJUTDhU`DJXEWPbKDhU`[E[Y[[YTDJWERZP!

    2.2. Matrices and Vectors!G!$atriD!is!a!set!of!&u$2ers!arra&6e'!i&!a!recta&6ular!6ri'!of!ro*s!a&'!colu$&sE!Bcalar!#!a!\!WEY_!or!a!\!]WEY^_!jector!#!2!\!]REY!,!WER^_!or!2!\!]REY!!WER^_!!T9e!5re1ious!eDa$5le!is!a!ro*!1ectorE!Nt!ca&!2e!a!colu$&!1ector!as!*ell,!suc9!as,!2!\!]REY! or!! 2!\!]REY!_!WER^_!!!!!!!!WER^_!

    +atriD!#!c!\! !ca&!also!2e!*ritte&!as!c!\!]JR![![_!R!R![_!R!iR![_![![!U^_!

    R [ [R R [R R [[ [ U

    %) *+ ,+

    %+ ,+ ,- .

    ,

    or!c!\!]JR,![,![_!R,!R,![_!R,!iR,![_![,![,!U^_!or!c!\!]JR!![!![!!!!or!!c!\!]JR,![,!![!!!!!! ! ! ! ! ! !!!!!!R!!R!![!! !!!!!!!!!!R,!R,!![!! ! ! ! ! ! !!!!!!R!iR![!! !!!!!!!!!!R,!JR,![!! ! ! ! ! ! !!!!!![!![!!U^_!!!!!!!!!!!!!!!![,![,!!U^_!!T9e!a2o1e!$atriD!9as!Z!ro*s!a&'!W!colu$&sE!G&?!ele$e&t!of!t9e!$atriD!is!'e&ote'!2?!cKi,!kP!*9ere!i!is!t9e!ro*!l!a&'!k!is!t9e!colu$&!lE!!

    !" T9e!co$$a&'!size(a) 6i1es!t9e!siQe!of!a!s5ecific!$atriD!aE!a\]R:Z_R[:RW^_!!size(a) *ill!6i1e!us!t9e!follo*i&6!out5ut:!a&s!\!

    U! Z!!

    T9e!Mu$2er!of!Ro*s!is!co&si'ere'!t9e!first!'i$e&sio&!K9e&ce!UP!a&'!t9e!&u$2er!of!colu$&s!is!co&si'ere'!t9e!seco&'!'i$e&sio&!K9e&ce!ZPE!!T9e!'i$e&sio&!!!

    !" T9e!co$$a&'!length(a)!retur&s!t9e!le&6t9!of!a!1ector!or!t9e!lo&6est!'i$e&sio&!of!a!UJA!arra?!a\]R:Z_R[:RW^_!length(a)!*ill!6i1e!us!t9e!follo*i&6!out5ut:!a&s!\!!Z!!2\]R:Y^_!length(b)!*ill!6i1e!us!t9e!follo*i&6!out5ut:!a&s!\!!Y!!

    ! c

  • CEE15 Introduction to Computing for Civil Engineers !

    !!!!

    Table 2.2 Special symbols related to array operations Character Description

    :! "olo&_!creates!1ectors!*it9!eIuall?!s5ace'!ele$e&ts,!!re5rese&ts!ra&6e!of!ele$e&ts!i&!arra?s!\! Gssi6&$e&t!o5erator!KP! Bu2scri5tio&s!of!arra?s!]^! Hrac@ets_!for$s!arra?s,!e&closes!out5ut!ar6u$e&ts!i&!fu&ctio&s!

    ,! "o$$a_!se5arates!arra?!su2scri5tio&s!a&'!fu&ctio&!ar6u$e&ts,!!se5arates!co$$a&'s!i&!t9e!sa$e!li&e!_! Be$icolo&_!su55resses!'is5la?,!e&'s!ro*!i&!arra?!m! Bi&6le!Iuote_!$atriD!tra&s5ose,!creates!stri&6!

    2.3. Special Matrices: magic: T9e!magic fu&ctio&!6e&erates!a&!&!'!&!$atriD!co&structe'!fro$!t9e!i&te6ers!fro$!R!t9rou69!&UE!T9e!i&te6ers!are!or'ere'!i&!suc9!a!*a?!t9at!all!t9e!ro*!su$s!a&'!all!t9e!colu$&!su$s!are!eIual!to!t9e!sa$e!&u$2erE!zeros: T9e!zeros fu&ctio&!6e&erates!a!$atriD!co&tai&i&6!all!QerosE!ones: T9e ones fu&ctio& 6e&erates!a!$atriD!co&tai&i&6!all!o&esE!!eye: T9e eye fu&ctio&!6e&erates!a&!i'e&tit?!$atriDE! "o&si'er!t9e!follo*i&6!5ro6ra$:!disp(magic(3)) disp('matrix of zeros') a=zeros(3) b=zeros(3,2) c=[1 2 3 ; 4 2 5] d=zeros(size(c)) disp('matrix of ones') a=ones(3) b=ones(3,2) c=[1 2 3;4 2 5] d=ones(size(c)) disp('identity matrix') a=eye(3) b=eye(3,2) d=eye(size(c)) T9e!out5ut!*ill!2e:! 8 1 6 3 5 7 4 9 2 matrix of zeros a = 0 0 0 0 0 0 0 0 0 b = 0 0 0 0 0 0

    ! n

  • CEE15 Introduction to Computing for Civil Engineers !

    c = 1 2 3 4 2 5 d = 0 0 0 0 0 0 matrix of ones a = 1 1 1 1 1 1 1 1 1 b = 1 1 1 1 1 1 c = 1 2 3 4 2 5 d = 1 1 1 1 1 1 identity matrix a = 1 0 0 0 1 0 0 0 1 b = 1 0 0 1 0 0 d = 1 0 0 0 1 0 2.4. Initializing Matrices T9ere!are!Z!$et9o's:!

    !" ED5licit!lists!!" Aata!file!!" "olo&!o5erator!!" Lser!i&5ut!

    2.4.1. Explicit Lists!c!\!]JR![![_!R!R![_!R!iR![_![![!U^_!!

    !" The name of the matrix must start with a letter!a&'!ca&!co&tai&!u5!to!Re!c9aracters!t9at!are!'i6its,!letters!a&'!t9e!u&'erscore!c9aracterE!

    !" (9e&!*e! 'efi&e! a!$atriD,!+GT>GH!*ill! 5ri&t! t9e! 1alue! of! t9e!$atriD! o&! t9e! &eDt! li&e!u&less!*e!su55ress!t9e!5ri&ti&6!*it9!t9e!se$icolo&!after!t9e!'efi&itio&E!

    !Nf!a!si&6le!ro*!1ector!is!too!lo&6!t9e&!*e!ca&!use!ellipsis () *9ic9!is!si$5l?!t9ree!5erio'sE!!!Examples:!7!\!]R!YU!XZ!Rec!ZU!iZU!YY!nU!UU!R[e^_!!ca&!2e!*ritte&!as,!7!\!]R!YU!XZ!Rec!ZU!iZU!V!! YY!nU!UU!R[e^_!!

    ! e

  • CEE15 Introduction to Computing for Civil Engineers !

    N&!fact,!ellipsis ca&!2e!use'!i&!a&?!li&e!of!a!+GT>GH!5ro6ra$!i&!or'er!to!s9o*!t9e!co&ti&uatio&E!!Concatenation: Nf!2!\!]REY!WER^!a&'!s!\!]WE[!2^!t9e&!s!$atriD!is!eIui1ale&t!to!]WE[!REY!WER^E!!!!!!!!!!!!(9at!a2out!6\]2a!2a^!a&'!6\]2a_2a^o!"o&si'er!t9e!follo*i&6!U!+GT>GH!state$e&ts:!!s!\!]WE[!REY!WER^_!sKUP!\!JRE[_!!T9e!&e*!s!$atriD!is!]WE[!iRE[!WER^E!!More examples: s!\!]WE[!iRE[!WER^_!sKZP!\!YEY_!sKnP!\!eEY_!!T9e!resulta&t!s!$atriD!is!s!\!]WE[!iRE[!WER!YEY![![![!eEY^E!T9is!eDa$5le!s9o*s!t9at!if!a''itio&al!e&tries!are!co&cate&ate'!2e?o&'!t9e!ori6i&al!ra&6e!of!t9e!$atriD,!t9e!&e*!$atriD!is!5a''e'!*it9!Qeros!i&!2et*ee&!t9ose!e&triesE!!!!2.4.2. Data File: +atrices!ca&!2e!'efi&e'!fro$!i&for$atio&!t9at!9as!2ee&!store'!i&!a!data fileE!+GT>GH!ca&!i&terface!to!U!'iffere&t!t?5es!of!data filesE!!

    !" MAT files: Aata!store'!i&!a!$e$or?Jefficie&t!2i&ar?!for$atE!T9e?!are!5refera2le!for!'ata!t9at!are!6oi&6!to!2e!6e&erate'!a&'!use'!2?!+GT>GH!5ro6ra$s!o&l?E!

    !" ASCII files: Aata!store'!i&!GB"NN!c9aractersE!T9e?!are!&ecessar?!if! t9e!'ata!are!to!2e!s9are'!Ki$5orte'!or!eD5orte'P!*it9!5ro6ra$s!ot9er!t9a&!+GT>GHE!

    2.4.2.1. Generating MAT Files: "o&si'er!t9e!follo*i&6!5ro6ra$E!a = [2 3]; b = [4 5]; c = a + b; d = a - b; save data c d; disp('c=') disp(c) disp('d=') disp(d) !T9e!a2o1e!5ro6ra$!*ill!auto$aticall?!6e&erate!a!data.mat fileE!T9e!out5ut!of!t9e!a2o1e!5ro6ra$!is:!c\!!!!!!X!!!!!n!'\!!!!!JU!!!!JU!!Mo*!co&si'er!a&ot9er!5ro6ra$!*9ic9!*ill!use!t9e!sa1e'!1alue!fro$!t9e!data.mat file!load data.mat; %Now data is a matrix c=data(1,:); d=data(2,:);

    ! R[

  • CEE15 Introduction to Computing for Civil Engineers !

    e = c + d; disp('e=') disp(e) !T9e!out5ut!of!t9e!a2o1e!5ro6ra$!*ill!2e:!e\!!!!!!Z!!!!!X!!!!!!

    !" save!filename!x y z!!-append!!a''s!t9e!1aria2les!to!a&!eDisti&6!MAT fileE! 2.4.2.2. Generating ASCII Files: Rules:

    !" The ASCII files $ust!co&tai&!o&l?!&u$eric!i&for$atio&E!(e!ca&!use!% i&!t9e!ASCII file for!co$$e&ts!li&esE

    !" Eac9! ro*! of! t9e!ASCII files $ust! co&tai&! t9e! sa$e! l! of! 'ata! 1alues! to! 2e! rea'! 2?! a&ot9er!5ro6ra$!i&!+GT>GHE

    !" ASCII files are!6e&erate'!i&!W!*a?s: 2.4.2.2.1. By a MATLAB program: "o&si'er!t9e!follo*i&6!5ro6ra$:!a = [2 3]; b = [4 5]; c = a + b; d = a - b; save data.dat c d -ascii; disp('c=') disp(c) disp('d=') disp(d) Nt!*ill!create!t9e!follo*i&6!out5ut:!c\!!!!!!X!!!!!n!'\!!!!!JU!!!!JU!!Nt!*ill!also!create!a&!ASCII file calle'!data.dat!as!t9e!follo*i&6:!6.0000000e+000 8.0000000e+000 -2.0000000e+000 -2.0000000e+000!!"o&si'er!a&ot9er!5ro6ra$!*9ic9!*ill!use!t9e!'ata!store'!i&!t9e!data.dat fileE!load data.dat; x = data(1,:); y = data(2,:); e = x + y; disp('e=') disp(e) !T9e!out5ut!of!t9e!a2o1e!5ro6ra$!is:!e\!!!!!!Z!!!!!X!

    ! RR

  • CEE15 Introduction to Computing for Civil Engineers !

    !Note:

    !" H?!loa'i&6!t9e!ascii!file,!t9e!'ata!1alue!*ill!2e!auto$aticall?!store'!i&!a!$atriD!data K*it9!t9e!sa$e!&a$e!as!t9e!'ata!fileP!*9ic9!*ill!9a1e!t9e!sa$e!siQe!as!t9e!'ataE

    !" T9ou69!1alues!of!t9e!1aria2les!c a&'!d *ere!store'!i&!t9e!data.dat file,!t9e?!ca&!2e!rea'!as!a!1aria2le!$atriD!Kdata)!for!our!caseE

    2.4.2.2.2. By a Text Editor or Word Processor: Lsi&6!a!teDt!e'itor,!create!a!&e*!file!as!follo*s:!X! n!JU! JU!!Ba1e!t9e!file!as!data1.datE!Mo*!*rite!t9e!follo*i&6!+GT>GH!5ro6ra$:!load data1.dat; disp(data1) x = data1(1,:); disp(x) y = data1(2,:); disp(y) e = x + y; disp('e=') disp(e) !Nt!*ill!6i1e!us!a&!out5ut!of:!!!!!!!X!!!!!n!!!!!JU!!!!JU!!!!!!!X!!!!!n!!!!!JU!!!!JU!!e\!!!!!!Z!!!!!X!!2.4.2.2.3. ASCII files ca&! also! 2e! 6e&erate'! 2?! a! Fortran or! C 5ro6ra$! a&'! it! ca&! 2e! use'! 2?!+GT>GH!5ro6ra$sE!!!2.4.2.3. Colon Operator: Array Indexing T9e!colo&!o5erator!creates!1ectors!fro$!a!$atriD!2?!selecti1el?!re$o1i&6!ro*s!a&'!colu$&sE!!Nt!sta&'s!for!pGll!Ro*sq!or!pGll!"olu$&sEq!!Nt!also!9as!ot9er!usesE! load data1.dat; disp(data1) x = data1(1,:); disp(x) y = data1(2,:); disp(y) e = x + y; disp('e=') disp(e) !T9e!colo&!o5erator!create'!x a&'!y 1ectors!fro$!t9e!data1 $atriDE!9!\!R:n_!is!eIui1ale&t!to!t9e!follo*i&6!ro*!1ector!]R!U!W!Z!Y!X!c!n^E!

    ! RU

  • CEE15 Introduction to Computing for Civil Engineers !

    9!\!R:U:e_!is!eIui1ale&t!to!]R!W!Y!c!e^!*it9!t9e!middle value K\UP!i&!t9e!eD5ressio&!9!\!R:U:e_!is the step incrementE!!!" T9e!colo&!o5erator!ca&!also!select!a!su2!$atriD!fro$!a&ot9er!$atriDE!7or!eDa$5le,!c = [-1 0 0; 1 1 0; 1 -1 0; 0 0 2]; disp('c=') disp(c) c1 = c(:,2:3); c2 = c(3:4,1:2); disp('c1=') disp(c1) disp('c2=') disp(c2)

    !T9e!a2o1e!5ro6ra$!*ill!9a1e!t9e!follo*i&6!out5ut:!c\!!!!!JR!!!!![!!!!![!!!!!!R!!!!!R!!!!![!!!!!!R!!!!JR!!!!![!!!!!![!!!!![!!!!!U!cR\!!!!!![!!!!![!!!!!!R!!!!![!!!!!JR!!!!![!!!!!![!!!!!U!cU\!!!!!!R!!!!JR!!!!!![!!!!![!!

    !" a!\! ]^_!*9ere!a is!a&!e$5t?!$atriDE!E$5t?!$atriD! is!'iffere&t! fro$!$atriD! t9at!co&tai&s!o&l?!QerosE!

    "o&si'er!t9e!follo*i&6!5ro6ra$:!c = [-1 0 0; 1 1 0; 1 -1 0; 0 0 2]; disp('c=') disp(c) x = c(:); disp('x=') disp(x)

    !Nt!*ill!6i1e!us!t9e!follo*i&6!out5ut:!c\!!!!!JR!!!!![!!!!![!!!!!!R!!!!!R!!!!![!!!!!!R!!!!JR!!!!![!!!!!![!!!!![!!!!!U!!D\!!!!!JR!!!!!!R!!!!!!R!

    ! RW

  • CEE15 Introduction to Computing for Civil Engineers !

    !!!!![!!!!!![!!!!!!R!!!!!JR!!!!!![!!!!!![!!!!!![!!!!!![!!!!!!U!!

  • CEE15 Introduction to Computing for Civil Engineers !

    Eb! Ele$e&tJ2?Jele$e&t!ri69t!'i1isio&!Eg! Ele$e&tJ2?Jele$e&t!left!'i1isio&!Eh! Ele$e&tJ2?Jele$e&t!eD5o&e&tiatio&!

    Precedence (higher to lower): RE!tra&s5ose!K.P,!5o*er!K.^P,!co$5leD!co&ku6ate!tra&s5ose!K'P,!$atriD!5o*er!K^P!UE!u&ar?!o5erator:!lo6ical!&e6atio&!K~P!WE!$ulti5licatio&!K.*P,!ri69t!'i1isio&!K./P,!left!'i1isio&!K.\P,!$atriD!$ulti5licatio&!K*P,!$atriD!ri69t!'i1isio&!

    K/P,!$atriD!left!'i1isio&!K\P!ZE!a''itio&!K+P,!su2tractio&!K-P!YE!colo&!o5erator!K:P!!"7or!t9e!o5erators!*it9!t9e!sa$e!5rece'e&ce,!t9e!eDecutio&s!5rocee'!fro$!left!to!ri69tE!!"o&si'er!t9e!follo*i&6!5ro6ra$:!a=[1 3 2; 3 5 4; 2 5 4]; b=[2 3 1;6 8 1; 9 7 5]; c=a.*b d=a*b (e!*ill!9a1e!t9e!follo*i&6!out5ut:!c = 2 9 2 18 40 4 18 35 20 d = 38 41 14 72 77 28 70 74 27 !" (e!9a1e! to!2e!1er?!careful! so! t9at! t9e!$atriD!'i$e&sio&s!a6ree! i&!2ot9! t9e!arra?!a&'! t9e!$atriD!

    o5eratio&sE!!" G&! arra?! o5eratio&! ca&! 2e! 2et*ee&! a! scalar! a&'! a! &o&scalar! K1ector! or! $atriDP! 2ut! t9e! $atriD!

    o5eratio&!9as!to!2e!2et*ee&!&o&scalarsE!!"o&si'er!t9e!follo*i&6!5ro6ra$:!a=[2 4 7]; b=3*a b=3.*a c=a/5 c=a./5 Nt!*ill!6i1e!us!t9e!follo*i&6!out5ut:!b = 6 12 21 b = 6 12 21 c = 0.4000 0.8000 1.4000 c = 0.4000 0.8000 1.4000 !" T9e!5oi&t!to!&ote!i&!t9e!a2o1e!5ro6ra$!is!t9at!*9e&!*e!co&si'er!o5eratio&s!2et*ee&!a!scalar!a&'!a!

    $atriD,!t9e!result!is!sa$e!for!2ot9!t9e!arra?!K.* a&'!./P!a&'!$atriD!K* a&'!/P!co$$a&'sE!!" T9e! arra?! o5eratio&s! for! t9e! 1ectors! a&'! t9e!$atrices! are! al$ost! si$ilarE!"o&si'er! t9e! follo*i&6!

    5ro6ra$:!

    ! RY

  • CEE15 Introduction to Computing for Civil Engineers !

    a=[2 5 6] b=[2 3 5] c=a.*b d=a./b e=a.\b f=a.^2 g=a.^b h=3.0.^a k=[1:5;-1:-1:-5] l=ones(size(k)) m=k-l n=k.*m o=k.^3 T9e!out5ut!s9oul'!loo@!li@e!t9e!follo*i&6:!a = 2 5 6 b = 2 3 5 c = 4 15 30 d = 1.0000 1.6667 1.2000 e = 1.0000 0.6000 0.8333 f = 4 25 36 g = 4 125 7776 h = 9 243 729 k = 1 2 3 4 5 -1 -2 -3 -4 -5 l = 1 1 1 1 1 1 1 1 1 1 m = 0 1 2 3 4 -2 -3 -4 -5 -6 n = 0 2 6 12 20 2 6 12 20 30 o = 1 8 27 64 125 -1 -8 -27 -64 -125 2.5.2. Common Functions: "o&si'er!t9e!follo*i&6!+GT>GH!state$e&t:!2!\!si&KDP_!!sin(x):!calculates!t9e!si&e!of!t9e!!a&6le!x!Kra'ia&sPE!!" T9e!a2o1e!state$e&t!is!1ali'!if!a&!a&6le!is!a!scalar!or!if!a&!a&6le!is!a!$atriDE!If angle is a matrix,

    then the function will be applied element by element to the values in the matrixE!!" G!fu&ctio&!ca&!9a1e!&o!ar6u$e&ts!KpiP,!o&e!ar6u$e&t!Ksin(angle)P!or!$ore!ar6u$e&ts!Krem(x,y)PE!!" G!fu&ctio&!ca&!also!2e!&este'!as,!?!\!lo6Ka2sKDPPE!!"o&si'er!t9e!follo*i&6!5ro6ra$:!x=[2.4 0.0 5.5; -1.2 -0.3 -1.7]

    ! RX

  • CEE15 Introduction to Computing for Civil Engineers !

    abs_x=abs(x) %It computes the absolute value of x. sqrt_x=sqrt(x) %It computes the square root of x. round_x=round(x) %It rounds x to nearest integer. fix_x=fix(x) %It rounds x to the nearest integer toward zero. floor_x=floor(x) %It rounds x to the nearest integer toward minus infinity. ceil_x=ceil(x) %It rounds x to the nearest integer toward infinity. sign_x=sign(x) %It returns -1 if x0. exp_x=exp(x) %It returns the value of e to the power x. log_x=log(x) %It returns the natural logarithm of x to the base e. log10_x=log10(x) %It returns the common logarithm of x to the base 10. a=[4 5];b=[2 2]; rem_ab=rem(a,b) %It returns the remainder of a/b. rut5ut:!x = 2.4000 0 5.5000 -1.2000 -0.3000 -1.7000 abs_x = 2.4000 0 5.5000 1.2000 0.3000 1.7000 sqrt_x = 1.5492 0 2.3452 0 + 1.0954i 0 + 0.5477i 0 + 1.3038i round_x = 2 0 6 -1 0 -2 fix_x = 2 0 5 -1 0 -1 floor_x = 2 0 5 -2 -1 -2 ceil_x = 3 0 6 -1 0 -1 sign_x = 1 0 1 -1 -1 -1 exp_x = 11.0232 1.0000 244.6919 0.3012 0.7408 0.1827 Warning: Log of zero. > In C:\matlab_sv12\work\Untitled1.m at line 10 log_x = 0.8755 -Inf 1.7047 0.1823 + 3.1416i -1.2040 + 3.1416i 0.5306 + 3.1416i Warning: Log of zero. > In C:\matlab_sv12\toolbox\matlab\elfun\log10.m at line 13 In C:\matlab_sv12\work\Untitled1.m at line 11 log10_x = 0.3802 -Inf 0.7404 0.0792 + 1.3644i -0.5229 + 1.3644i 0.2304 + 1.3644i rem_ab =

    0 1 2.5.3. Transpose of a Matrix: T9e!tra&s5ose!of!a!$atriD!is!a!&e*!$atriD!i&!*9ic9!t9e!ro*s!of!t9e!ori6i&al!$atriD!are!t9e!colu$&s!of!t9e!&e*!$atriDE! Nf! a!$atriD!co&tai&s!a!co$5leD!1alue! t9e&!*e!ca&!9a1e!2ot9! t9e!co$5leD!co&ku6ate!tra&s5ose! a&'! co$5leD! &o&co&ku6ate! tra&s5oseE! T9e! follo*i&6! 5ro6ra$! s9oul'! eD5lai&! t9ese! ter$s!$ore!clearl?E!

    !Program:

    ! Rc

  • CEE15 Introduction to Computing for Civil Engineers !

    disp('The original Matrices') a = [ 2i+3 3; 5 4i-5] aa = [2 3;5 -4] disp('The nonconjugated transpose :') disp('There are three ways to compute this.') b = transpose(a) %complex c = a.' %complex d = conj(a') %complex e = aa.' %noncomplex disp('The conjugated transpose :') disp('There are two ways to compute this.') f = a' %complex g = ctranspose(a) %complex h = aa.' %noncomplex Output: The original Matrices a = 3.0000 + 2.0000i 3.0000 5.0000 -5.0000 + 4.0000i aa = 2 3 5 -4 The nonconjugated transpose : There are three ways to compute this. b = 3.0000 + 2.0000i 5.0000 3.0000 -5.0000 + 4.0000i c = 3.0000 + 2.0000i 5.0000 3.0000 -5.0000 + 4.0000i d = 3.0000 + 2.0000i 5.0000 3.0000 -5.0000 + 4.0000i e = 2 5 3 -4 The conjugated transpose : There are two ways to compute this. f = 3.0000 - 2.0000i 5.0000 3.0000 -5.0000 - 4.0000i g = 3.0000 - 2.0000i 5.0000 3.0000 -5.0000 - 4.0000i h = 2 5 3 -4

    2.5.4. Dot Product ---- dot(a,b): T9e!'ot!5ro'uct!is!t9e!scalar!co$5ute'!fro$!t*o!1ectors!of!t9e!sa$e!siQeE!

    R

    s K , Pn

    i ii

    do t product do t a b a b/

    / / 0N&!+GT>GH,!'ot!5ro'uct!ca&!2e!also!co$5ute'!as,!sum(a.*b),!*9ere!2ot9!a!a&'!b are!eit9er!ro*!or!colu$&!1ectorsE!Nf!a is!a!ro*!1ector!a&'!b is!a!colu$&!1ector,!t9e&!t9e!co$5utatio&!coul'!2e!'o&e!*it9!eit9er!of!t9e!follo*i&6!co$$a&'s:!!!sum(a.*b) or!!

    ! Rn

  • CEE15 Introduction to Computing for Civil Engineers !

    b

    sum(a.*b)E!!or!if!a!a&'!2e!are!2ot9!colu$&!1ectors!?ou!$a?!*rite!aaT2E!!T9is!is!t9e!mi&&er!5ro'ucta!a&'!5ro'uces!a!scalar!result!o&!t9e!ot9er!9a&':!aT2a!!!6i1es!t9e!mruter!5ro'ucta!a&'!5ro'uces!a!$atriD!resultE!!2.5.5. Cross Product ---- cross(a,b): T9e! co$$a&'!cross(a,b)! retur&s! t9e! cross! 5ro'uct! of! t9e! 1ectors!a! a&'!bE!Hot9!a! a&'!b!$ust! 2e! W!ele$e&t!1ectorsE!!2.5.6. Matrix Multiplication ---- a*b:

    R

    Tn

    ij ik k jk

    c a b

    c a/

    /

    / 0T9e!first!$atriD!a $ust!9a1e!t9e!sa$e!&u$2er!of!ele$e&ts!i&!eac9!ro*!as!t9ere!are!i&!t9e!colu$&s!of!t9e!seco&'!$atriD!bE!T9e!resulta&t!$atriD!*ill!9a1e! t9e!sa$e!&u$2er!of!ro*s!of! t9e!first!$atriD!a&'!sa$e!&u$2er!of!colu$&s!as!t9e!seco&'!$atriDE! !(e!&ote!t9at! t9e!mi&&er!'i$e&sio&a!of!2ot9!$atrices!$ust!2e!t9e!sa$eE!!G&!eDa$5le!is!G]WDY^!T!H]YDR[^!\!"]WDR[^E!Note: Nf! f a&'! g are! t*o! ro*! 1ectors,! t9e! 'ot! 5ro'uct! $a?! also! 2e! co$5ute'! as! f*g K+atriD!

    $ulti5licatio&PE! 2.5.7. Matrix Powers ---- a^n: T9e!co$$a&'!for!t9e!5o*er!of!a!$atriD!a is!a^2!K*9ere,!5o*er!is!eIual!to!UPE!a^2 is!eIui1ale&t!to!a*aE!Bi$ilarl?,!a^4 is! eIui1ale&t! to!a*a*a*aE!To! raise! a!$atriD! to! a! 5o*er,! t9e!$atriD!$ust! 2e! a! sIuare!$atriDE!!2.5.8. Matrix Inverse ---- inv(a): H?!'efi&itio&,! if!b is! a&! i&1erse!of! a! sIuare!$atriD!a,! t9e&!a*b or!b*a! are!2ot9!eIual! to!a&! i'e&tit?!$atriD!*it9!o&l?!t9e!'ia6o&al!ele$e&ts!2ei&6!R!a&'!ot9er!ele$e&ts!2ei&6![E!

    Program: a = [4 -1 3] b = [-2 5 2] dot_ab1 = dot(a,b) dot_ab2 = a*b' cross_ab = cross(a,b) c = [2 5 1;0 3 -1] d = [1 0 2;-1 4 -2;5 2 1] mult_cd = c*d matrix_power1 = d^2 matrix_power2 = d*d matrix_inverse = inv(d) matrix_invmatrix = d*matrix_inverse Output: a = 4 -1 3 b = -2 5 2

    ! Re

  • CEE15 Introduction to Computing for Civil Engineers !

    dot_ab1 = -7 dot_ab2 = -7 cross_ab = -17 -14 18 c = 2 5 1 0 3 -1 d = 1 0 2 -1 4 -2 5 2 1 mult_cd = 2 22 -5 -8 10 -7 matrix_power1 = 11 4 4 -15 12 -12 8 10 7 matrix_power2 = 11 4 4 -15 12 -12 8 10 7 matrix_inverse = -0.2222 -0.1111 0.2222 0.2500 0.2500 0.0000 0.6111 0.0556 -0.1111 matrix_invmatrix = 1.0000 0 0.0000 -0.0000 1.0000 0.0000 -0.0000 -0.0000 1.0000

    2.5.9. Determinants ---- det(a): Program: a = [2 4 5;1 2 3;0 2 4] det_a = det(a) Output: a = 2 4 5 1 2 3 0 2 4 det_a = -2

    2.6. Formatted Printing of Matrices 2.6.1. Long-Short format N&!+GT>GH! t9e! 'eci$al! fractio&s! are! 5ri&te'! usi&6! a! 'efault! for$at! Kshort formatP! t9at! s9o*s! Y!si6&ifica&t!'eci$al!'i6itsE! Nf!*e!*a&t!1alues! to!2e!'is5la?e'! i&!a!'eci$al! for$at!*it9!RY!si6&ifica&t!'i6its,! *e! use! t9e! co$$a&'! format longE! T9e! for$at! ca&! 2e! retur&e'! to! a! 'eci$al! for$at! *it9! Y!si6&ifica&t!'i6its!usi&6!t9e!co$$a&'!format shortE!7or!eDa$5le:!a = [1.23456789 2.34567891]; disp(a); !(it9!format short,!t9e!a2o1e!5ro6ra$!*ill!5ri&t,!REUWZX! UEWZYc!

    ! U[

  • CEE15 Introduction to Computing for Civil Engineers !

    !a&'!*it9!format long,!REUWZYXcne[[[[[[!!!UEWZYXcneR[[[[[[!!Mo*!co&si'er!a!si$ilar!5ro6ra$!as!t9e!follo*i&6:!a = [1.23456789 2.34567891]; disp(a); format long; disp(a); The output will be: !REUWZX!!!!UEWZYc!REUWZYXcne[[[[[[!!!UEWZYXcneR[[[[[[! ! !!

    !" format short e co$$a&'!*ill!5ri&t!t9e!1alues!i&!scie&tific!&otatio&!*it9!Y!si6&ifica&t!'i6its!a&'!format long e 5ri&ts!t9e!sa$e!2ut!*it9!RY!si6&ifica&t!'i6itsE!7or!eDa$5le:!

    a = [1234.5678934567 234.56789198]; disp(a);

    The above program will print (with the format short e command) 1.2346e+003 2.3457e+002

    and the same will print with the format long e command. 1.234567893456700e+003 2.345678919800000e+002

    !!" Format + co$$a&'!K5ri&t!t9e!si6&!o&l?P:!

    c = [-1 0 1; 1 1 0; 1 -1 0; 0 0 2];disp(c) format +; disp(c) The output of the above program will be: -1 0 1 1 1 0 1 -1 0 0 0 2 - + ++ +- +!!(9e&!a!$atriD! is!5ri&te'!*it9! t9e! format + co$$a&',! t9e!o&l?!c9aracter!5ri&te'! is!5lus!a&'!$i&us!si6&sE!Nf!a!1alue!is!5ositi1e!a!5lus!si6&!*ill!2e!5ri&te'_!if!a!1alue!is!Qero,!a!s5ace!*ill!2e!5ri&te'_!if!a!1alue!is!&e6ati1e,!a!$i&us!si6&!*ill!2e!5ri&te'E!!

    Table 2.4 Display formats Command Description 7or$at!2a&@! T*o!'eci$al!'i6its!

    7or$at!co$5act! Eli$i&ates!e$5t?!li&es!7or$at!lo&6! 7iDe'J5oi&t!for$at!*it9!RZ!'eci$al!'i6its!7or$at!lo&6!e! Bcie&tific!&otatio&!*it9!RY!'eci$al!'i6its!7or$at!lo&6!6! Hest!of!RYJ'i6it!fiDe'!or!floati&6!5oi&t!

    ! UR

  • CEE15 Introduction to Computing for Civil Engineers !

    7or$at!loose! G''s!e$5t?!li&es!7or$at!s9ort! 7iDe'J5oi&t!for$at!*it9!Z!'eci$al!'i6its!7or$at!s9ort!e! Bcie&tific!&otatio&!*it9!Z!'eci$al!'i6its!7or$at!s9ort!6! Hest!of!YJ'i6it!fiDe'!or!floati&6!5oi&t!

    2.6.2. Prescribed Formatted Printing

    !" disp co$$a&':!!

    disp('Total # of students in the class =') n = 20; disp(n) !T9e!5ro6ra$!a2o1e!*ill!6i1e!us!t9e!follo*i&6!out5ut:!Total # of students in the class = 20 !

    !" 7or$atte'!out5ut!Kfprintf co$$a&'P:!T9e!s5ecifiers:!!!%e #!ED5o&e&tial!&otatio&!%f #!7iDe'!5oi&t!or!'eci$al!&otatio&!%g #!T9e!1alues!*ill!eit9er!use!%e or!%f,!'e5e&'i&6!o&!*9ic9!is!s9orter!!

    "o&si'er!t9e!follo*i&6!5ro6ra$:!temp = 78.234567989; fprintf('The temperature is %f degrees',temp); Nt!*ill!5ri&t!The temperature is 78.234568 degrees Mo*!co&si'er!t9is:!temp = 78.234567989; fprintf('The temperature is %e degrees',temp); Nt!*ill!5ri&t!The temperature is 7.823457e+001 degrees Mo*,!!temp = 78.234567989; fprintf('The temperature is %g degrees',temp); *ill!6i1e!us!a&!out5ut!!The temperature is 78.2346 degrees Note:!Nf!t9e!stri&6!\n!a55ears!i&!t9e!for$at,!t9e!li&e!s5ecifie'!u5!to!t9at!5oi&t!is!5ri&te',!a&'!t9e!rest!of!t9e!i&for$atio&!*ill!2e!5ri&te'!i&!t9e!&eDt!li&eE! temp = 78.234567989; fprintf('The temperature is %g degrees.',temp) fprintf('It is warm.') T9e!5ro6ra$!a2o1e!*ill!6i1e!us!a&!out5ut!as!t9e!follo*i&6:!

    ! UU

  • CEE15 Introduction to Computing for Civil Engineers !

    The temperature is 78.2346 degrees.It is warm. (9ere!as,!!temp = 78.234567989; fprintf('The temperature is %g degrees.\n',temp) fprintf('It is warm.') *ill!5ri&t:!The temperature is 78.2346 degrees. It is warm. G&',!fprintf('The temperature is %g degrees.\nIt is warm.',temp) *ill!5ri&t:!The temperature is 78.2346 degrees. It is warm. !Note:!T9e!for$at!s5ecifiers!%f,!%e a&'!%g ca&!also!co&tai&!i&for$atio&!to!s5ecif?!t9e!&u$2er!of!'eci$al!5laces!to!5ri&t!a&'!t9e!&u$2er!of!5ositio&s!to!allot!for!t9e!corres5o&'i&6!1alueE!!temp = 78.234567989; fprintf('The temperature is %4.1f degrees.\n',temp) fprintf('It is warm.') !T9e!5ro6ra$!a2o1e!*ill!6i1e!us!t9e!follo*i&6!out5ut:!The temperature is 78.2 degrees. It is warm. N&!t9e!a2o1e!5ro6ra$!t9e!out5ut!*ill!co&tai&!t9e!1alue!of!temp 5ri&te'!*it9!Z!5ositio&s,!o&e!of!*9ic9!*ill!2e!a!'eci$al!5ositio&!as!s9o*&!a2o1eE!

    ! UW

  • CEE15 Introduction to Computing for Civil Engineers !

    Chapter 3 Branching Statements

    3.1. Relational Operators +GT>GH!9as!siD!relational operators for!co$5ari&6!t*o!$atrices!of!eIual!siQe!or!t*o!scalarsE!

    Table 3.1 Relational operators!Relational Operator Interpretation

    t! >ess!t9a&!t\! >ess!t9a&!or!eIual!to!u! vreater!t9a&!u\! vreater!t9a&!or!eIual!to!\\! EIual!to!w\! Mot!eIual!to!

    !"o&si'er!t9e!follo*i&6!5ro6ra$:!a = input('a ='); b = input('b ='); if a > b disp('a is greater than b') end if a < b disp('b is greater than a') end if a == b disp('a is equal to b') end rut5ut!R:!a =3 b =4 b is greater than a rut5ut!U:!a =4 b =2 a is greater than b rut5ut!W:!a =5 b =5 a is equal to b rut5ut!Z:!a =[2 3 5] b =[3 7 9] b is greater than a !rut5ut!Y:!a = [2 4 5] b = [1 3 6] !!" (9e&!*e! co$5are! t*o!$atrices! or! 1ectors! usi&6! a! relatio&al! o5erator,! t9e! logical expression is!

    o&l?!true!if!all! t9e!ele$e&ts!satisf?!t9e!co&'itio&!i&'i1i'uall?E!7or!eDa$5le,!2ot9!a a&'!b 1ectors!9a1e!W!ele$e&tsE!N&!or'er! to!9a1e!t9e!lo6ical!eD5ressio&!a > b to!2e!true,!all! t9e!W!ele$e&ts!of!a $ust!2e!6reater!t9a&!t9e!corres5o&'i&6!ele$e&ts!of!bE!

    ! UZ

  • CEE15 Introduction to Computing for Civil Engineers !

    !" Gll!t9e!relatio&al!o5erators!9a1e!t9e!sa$e!5rece'e&ceE!!"o&si'er!t9e!follo*i&6!5ro6ra$!a&'!its!out5ut!!!a = input('a ='); b = input('b ='); if a > b disp('a is greater than b') end if a < b disp('b is greater than a') end if a == b disp('a is equal to b') end rut5ut!:!a = [2 3] b = [4 5] b is greater than a 3.2. Logical Operators (e!ca&!also!co$2i&e!t*o!lo6ical!eD5ressio&s!usi&6!t9e!logical operatorsE!

    Table 3.2 Logical Operators Logical Operator Symbol

    a&'! x!or! y!&ot! w!

    !!" T9e!9ierarc9?!fro$!9i69est!to!lo*est,!is!not,!and, a&'!orE!

    Table 3.3 Result of Logical Operators A B ~A A | B A & B

    false! false! true! false! false!false! true! true! true! false!true! false! false! true! false!true! true! false! true! true!

    3.3. Simple if Statement: if lo6ical!eD5ressio&! state$e&t!6rou5!a!end "o&si'er!t9e!follo*i&6!5ro6ra$!:!n = input('n=') count = 0; if n < 50 count = count + 1; disp('count') disp(count) end disp('count') disp(count) Output 1:!

    ! UY

  • CEE15 Introduction to Computing for Civil Engineers !

    n=30 n = 30 count 1 count 1 Output 2:!n=100 n = 100 count 0 3.4. Nested if Statement: if lo6ical!eD5ressio&!R! state$e&t!6rou5!a!! if lo6ical!eD5ressio&!U! state$e&t!6rou5!2!! end state$e&t!6rou5!c!end

    !!"o&si'er!t9e!follo*i&6!5ro6ra$:!n = input('Enter the value n = '); count = 0; if n < 50 count = count + 1; disp('count') disp(count) if n < 25 disp('n is less than 25') end disp('n is less than 50') end disp('count') disp(count) Output 1: Enter the value n = 100 count 0 Output 2: Enter the value n = 30 count 1 n is less than 50 count 1 Output 3: Enter the value n = 20 count 1 n is less than 25 n is less than 50 count

    ! UX

  • CEE15 Introduction to Computing for Civil Engineers !

    1 3.5. else Clause: if lo6ical!eD5ressio&!! state$e&t!6rou5!a!else ! state$e&t!6rou5!2!end Nf!t9e!lo6ical!eD5ressio&!is!true,!t9e&!state$e&t!6rou5!a!is!eDecute'E!Nf!t9e!lo6ical!eD5ressio&!is!false,!t9e&!state$e&t!6rou5!2!is!eDecute'E!!Example: a = input('a='); b = input('b='); counta=0;countb=0; if a > b disp('a is greater than b') counta = counta + 1; else disp('b is greater than or equal to a') countb = countb + 1; end counta countb disp('end of program') Output 1:!a=5 b=3 a is greater than b

    counta = 1 countb = 0 end of program

    Output 2:!a=4 b=6 b is greater than or equal to a counta = 0 countb = 1 end of program 3.6. elseif Clause: if lo6ical!eD5ressio&!R! state$e&t!6rou5!a!elseif lo6ical!eD5ressio&!U!! state$e&t!6rou5!2!elseif lo6ical!eD5ressio&!W! state$e&t!6rou5!c!end

    ! Uc

  • CEE15 Introduction to Computing for Civil Engineers !

    Nf!lo6ical!eD5ressio&!R!is!true,!t9e&!o&l?!state$e&t!6rou5!a!is!eDecute'E!Nf!lo6ical!eD5ressio&!R!is!false!a&'!lo6ical!eD5ressio&!U!is!true,!t9e&!o&l?!state$e&t!6rou5!2!is!eDecute'E!Nf!lo6ical!eD5ressio&s!R!a&'!U!are!false!a&'!lo6ical!eD5ressio&!W!is!true,!t9e&!o&l?!state$e&t!6rou5!c!is!eDecute'E!Nf!2ot9!t9e!lo6ical!eD5ressio&s!R!a&'!U!are!true,!t9e&!o&l?!state$e&t!6rou5!a!is!eDecute'E!Nf!&o&e!of!t9e!lo6ical!eD5ressio&s!is!true,!t9e&!&o&e!of!t9e!state$e&ts!*it9i&!t9e!if!structure!is!eDecute'E!!!Example: a = input('a='); b = input('b='); if a > b disp('a is greater than b') elseif a < b disp('b is greater than a') elseif a ==b disp('a is equal to b') end disp('end of program') T9e!5ro6ra$!a2o1e!*ill!6i1e!us!t9e!follo*i&6!out5uts:!Output 1: a=3 b=5 b is greater than a end of program !Output 2: a=5 b=2 a is greater than b end of program !Output 3: a=5 b=5 a is equal to b end of program "o&si'er!t9e!5ro6ra$!2elo*:!a = input('a='); b = input('b='); c = input('c='); if a > b disp('a is greater than b') elseif b > c disp('b is greater than c') elseif c > a disp('c is greater than a') end disp('end of program') Output 1:!a=3 b=6 c=2 b is greater than c end of program Output 2:!

    ! Un

  • CEE15 Introduction to Computing for Civil Engineers !

    a=3 b=2 c=1 a is greater than b end of program 3.7. elseif else Clause: if lo6ical!eD5ressio&!R! state$e&t!6rou5!a!elseif lo6ical!eD5ressio&!U!! !!!!!!!!!!!!!state$e&t!6rou5!2!elseif lo6ical!eD5ressio&!W! state$e&t!6rou5!c!else state$e&t!6rou5!'!end !Nf!lo6ical!eD5ressio&!R!is!true,!t9e&!o&l?!state$e&t!6rou5!a!is!eDecute'E!Nf!lo6ical!eD5ressio&!R!is!false!a&'!lo6ical!eD5ressio&!U!is!true,!t9e&!o&l?!state$e&t!6rou5!2!is!eDecute'E!Nf!lo6ical!eD5ressio&s!R!a&'!U!are!false!a&'!lo6ical!eD5ressio&!W!is!true,!t9e&!o&l?!state$e&t!6rou5!c!is!eDecute'E!Nf!2ot9!t9e!lo6ical!eD5ressio&s!R!a&'!U!are!true,!t9e&!o&l?!state$e&t!6rou5!a!is!eDecute'E!Nf!t9e!lo6ical!eD5ressio&s!R,!U!a&'!W!are!false,!t9e&!state$e&t!6rou5!'!is!eDecute'E!!Example: a = 5; b = 5; if a > b disp('a is greater than b') elseif b > a disp('b is greater than a') else disp('a is equal to b') end disp('end of program') Output: a is equal to b end of program

    ! Ue

  • CEE15 Introduction to Computing for Civil Engineers !

    Chapter 4 Loops

    4.1. for Loops: for i&'eD!\!eD5ressio&! state$e&t!6rou5!a!end !Rules for for Loops: !" T9e!i&'eD!for!t9e!for loo5!$ust!2e!a!1aria2leE !" Nf! t9e! eD5ressio&! $atriD! is! a! scalar,! t9e&! t9e! loo5! *ill! 2e! eDecute'! o&e! ti$e,! *it9! t9e! i&'eD!

    co&tai&i&6!t9e!1alue!of!t9e!scalarE !" Nf!t9e!eD5ressio&!$atriD!is!a!ro*!1ector,!t9e&!eac9!ti$e!t9rou69!t9e!loo5,!t9e!i&'eD!*ill!co&tai&!t9e!

    &eDt!1alue!i&!t9e!1ectorE !" Nf!t9e!eD5ressio&!$atriD!is!a!$atriD,!t9e&!eac9!ti$e!t9rou69!t9e!loo5,!t9e!i&'eD!*ill!co&tai&!t9e!&eDt!

    colu$&!i&!t9e!$atriDE !" L5o&!co$5letio&!of!t9e!loo5,!t9e!i&'eD!co&tai&s!t9e!last!1alue!use'E !" Nf!t9e!colo&!o5erator!is!use'!to!'efi&e!t9e!eD5ressio&!$atriD!usi&6!t9e!follo*i&6!for$at: for k = i&itial:i&cre$e&t:li$it!!t9e&!t9e!&u$2er!of!ti$es!t9e!loo5!*ill!2e!eDecute'!ca&!2e!co$5ute'!usi&6!t9e!follo*i&6!eIuatio&:!floorKKli$itJi&itialPbi&cre$e&tP`R!!Nf!t9e!1alue!is!&e6ati1e,!t9e!loo5!is!&ot!eDecute'E!!!Example: for!@!\!Y:Z:nW! T9e!loo5!*oul'!2e!eDecute'!U[!ti$es!*it9!t9e!fi&al!1alue!of!k 2ei&6!nRE!!Example: for k = 5 k = k*3 end !Output: k = 15 !Example:!for k = [5 4 9] x = k*3 end k Output: x = 15 x = 12 x =

    ! W[

  • CEE15 Introduction to Computing for Civil Engineers !

    27 k = 9 Program: for k = [5 4 9;1 2 3] x = k*3 end k Output: x = 15 3 x = 12 6 x = 27 9 k = 9 3 Example:!for k = 2:5 x = k*3 end k !Output: x = 6 x = 9 x = 12 x = 15 k = 5 Example:!for k = 2:3:11 k x = k*3 end Output: k k = 2 x = 6 k = 5 x = 15 k = 8 x = 24 k =

    ! WR

  • CEE15 Introduction to Computing for Civil Engineers !

    11 x = 33 k = 11 !" T9e!i&cre$e&t!ca&!also!2e!&e6ati1e,!for!eDa$5le,!for k = 5:-1:2! Example:!d = [2 4 7 9 13 20]; for k = 1:6 k if d(k)

  • CEE15 Introduction to Computing for Civil Engineers !

    4.2. while loop: while eD5ressio&! state$e&t!6rou5!a!!end Nf! t9e! eD5ressio&! is! true,! t9e! state$e&t! 6rou5 a is eDecute'E! Gfter! t9e! state$e&ts! are! eDecute',! t9e!co&'itio&!is!reteste'E!Nf!t9e!eD5ressio&!is!still!true,!t9e!6rou5!of!state$e&ts!is!eDecute'!a6ai&E!(9e&!t9e!co&'itio&!is!false,!t9e!co&trol!s9ifts!to!t9e!state$e&ts!follo*i&6!t9e!end state$e&tE!Nf!t9e!eD5ressio&!is!al*a?s!true,!t9e!loo5!2eco$es!a&!i&fi&ite!loo5!a&'!*e!&ee'!to!use!t9e!Ctrl-C!@e?!to!a2ort!itE!!T9e!follo*i&6!5ro6ra$!a''s! t9e!1alues! i&!a!1ector!x to!a!scalar!1aria2le!calle'!sum u&til!a!&e6ati1e!1alue!is!reac9e'E!!sum = 0; x = [2 5 7 5 9 0 -2 5 4] k = 1 while k = 0 sum = sum + x(k) k = k+1 end disp('The last value of k') k T9e!out5ut!of!t9e!5ro6ra$!a2o1e!is!as!follo*s:!x = 2 5 7 5 9 0 -2 5 4 k = 1 sum = 2 k = 2 sum = 7 k = 3 sum = 14 k = 4 sum = 19 k = 5 sum = 28 k = 6 sum = 28 k = 7 The last value of k k = 7 !Example:

    ! WW

  • CEE15 Introduction to Computing for Civil Engineers !

    sum = 0; x = [2 5 7] k = 1 while x(k) >= 0 sum = sum + x(k) k = k+1 end disp('The last value of k') k Output: k = 1 sum = 2 k = 2 sum = 7 k = 3 sum = 14 k = 4 ??? Index exceeds matrix dimensions. Error in ==> C:\matlabR12\work\Untitled.m On line 4 ==> while x(k) >= 0 !Example: sum = 0; x = [2 5 7] k = 1 while k = 0 sum = sum + x(k) k = k+1 end disp('The last value of k') k Output: k = 1 sum = 2 k = 2 sum = 7 k = 3 sum = 14 k = 4 The last value of k k = 4

    ! WZ

  • CEE15 Introduction to Computing for Civil Engineers !

    Chapter 5 Plotting Capabilities

    5.1. X Y Plot!rur!o2kecti1e!is!to!6e&erate!DJ?!5lot!fro$!'ata!store'!i&!U!1ectorsE!D!\!ti$e,!s! ! ?!\!te$5,!17!

    [! YZEU!R! YnEY!

    ! U! ! ! XWEn!W! ! ! XZEU!

    !T9ere!are!W!*a?s!to!rea'!t9is!'ataE!!

    !" T9e!'ata!ca&!2e!rea'!eD5licitl?E!7or!eDa$5le,!!x=[0 1 2 3]; y=[54.2 58.5 63.8 64.2]; disp(x); disp(y); plot(x,y),... title('Temperature Measurement'),... xlabel('time, s'),... ylabel('temp, F'),... grid !*ill!6i1e!t9e!follo*i&6!out5ut!i&!t9e!co$$a&'!*i&'o*:!0 1 2 3 54.2000 58.5000 63.8000 64.2000 a&'!t9e!follo*i&6!fi6ure!i&!t9e!6ra59ic!*i&'o*:!!

    !!

    !" T9e!'ata!ca&!2e!rea'!usi&6!t9e!user!i&5utE!7or!eDa$5le!co&si'er!t9e!follo*i&6!5ro6ra$:!!

    x=input('give the values of time'); y=input('give the values of temperature'); disp(x); disp(y); plot(x,y),... title('Temperature Measurement'),... xlabel('time, s'),... ylabel('temp, F'),... grid T9e!a2o1e!5ro6ra$!*ill!6i1e!us!t9e!follo*i&6!out5ut!i&!t9e!co$$a&'!*i&'o*:!

    ! WY

  • CEE15 Introduction to Computing for Civil Engineers !

    give the values of time [0 1 2 3] give the values of temperature [54.2 58.5 63.8 64.2] 0 1 2 3 54.2000 58.5000 63.8000 64.2000 Nt!*ill!also!6i1e!us!t9e!sa$e!5lot!i&!t9e!6ra59ic!*i&'o*E!

    !" T9e!'ata!ca&!2e! rea'! fro$!a&!ASCII file i&to!a!$atriD!a&'!store'! i&! t9e!1ectors!usi&6!colon operatorE!!

    !7or!eDa$5le,!let!us!co&si'er!t9e!follo*i&6!file!Kdata.datP:![!R!U!W!YZEU!YnEY!XWEn!XZEU!!a&'!t9e!follo*i&6!5ro6ra$:!load data.dat; disp(data); x=data(1,:); y=data(2,:); plot(x,y),... title('Temperature Measurement'),... xlabel('time, s'),... ylabel('temp, F'),... grid T9e!out5ut!i&!t9e!co$$a&'!*i&'o*!*ill!2e!:![!!!!RE[[[[!!!!UE[[[[!!!!WE[[[[!YZEU[[[!!!YnEY[[[!!!XWEn[[[!!!XZEU[[[!!a&'!t9e!5lot!re$ai&s!t9e!sa$eE!!

    !" (e!coul'!9a1e!also!store'!t9e!'ata!i&!a!mat file a&'!rea'!t9e!1alues!of!ti$e a&'!te$5erature!fro$!t9ereE!

    !(e!ca&!also!9a1e!t*o!6ra59ic!*i&'o*s!at!t9e!sa$e!ti$eE!7or!eDa$5le,!!!load data.dat; disp(data); x=data(1,:); y=data(2,:); plot(x,y),... title('Temperature Measurement'),... xlabel('time, s'),... ylabel('temp, F'),... grid plot(y,x),... title('Temperature Measurement'),... ylabel('time, s'),... xlabel('temp, F'),... grid *ill!6i1e!us!o&l?!seco&'!5lot!2ut!!load data.dat; disp(data); x=data(1,:);

    ! WX

  • CEE15 Introduction to Computing for Civil Engineers !

    y=data(2,:); figure(1) plot(x,y),... title('Temperature Measurement'),... xlabel('time, s'),... ylabel('temp, F'),... grid figure(2) plot(y,x),... title('Temperature Measurement'),... ylabel('time, s'),... xlabel('temp, F'),... grid *ill!6i1e!us!2ot9!t9e!5lotsE!!5.2. 2-D Plot Labels title(text)!#!T9is!co$$a&'!*rites!t9e!teDt!as!a!title!at!t9e!to5!of!t9e!curre&t!5lotE!xlabel(textP!#!T9is!co$$a&'!*rites!t9e!teDt!2e&eat9!t9e!DJaDis!o&!t9e!curre&t!5lotE!ylabel(text)!#!T9is!co$$a&'!*rites!t9e!teDt!2esi'e!t9e!?JaDis!o&!t9e!curre&t!5lotE!text(x,y,text)!#!T9is!co$$a&'!*rites!t9e!teDt!o&!t9e!6ra59ics!scree&!at!t9e!5oi&ts!s5ecifie'!2?!t9e!

    coor'i&ates! KD,?P!usi&6! t9e! aDes! fro$! t9e! curre&t!5lotE! Nf!x! a&'!y! are!1ectors! t9e&! t9e! teDt! is!*ritte&!i&!eac9!5oi&tE!

    gtext(text)!!#!T9is!co$$a&'!*rites!t9e!teDt!at!t9e!5ositio&!o&!t9e!6ra59ics!scree&!i&'icate'!2?!t9e!$ouse!or!arro*!@e?sE!

    grid !#!T9is!co$$a&'!a''s!6ri'!li&es!to!t9e!curre&t!5lotE! 5.3. Plot Commands plot(x,y)!#!T9e!co$$a&'!6e&erates!a! li&ear!5lot!*it9!x!2ei&6!t9e!i&'e5e&'e&t!1aria2le!a&'!y!2ei&6!

    t9e!'e5e&'e&t!1aria2leE!semilogx(x,y)!#!T9is!co$$a&'!6e&erates!a!5lot!*it9!lo6arit9$ic!scale!for!x!a&'!li&ear!scale!for!yE!semilogy(x,y) #!T9is!co$$a&'!6e&erates!a!5lot!*it9!lo6arit9$ic!scale!for!y!a&'!li&ear!scale!for!xE!loglog(x,y)!#!T9is!co$$a&'!6e&erates!a!5lot!*it9!lo6arit9$ic!scales!for!2ot9!x!a&'!yE! Program: time = [1 2 3 4 5] temp = [2 5 9 14 20] figure(1) plot(time,temp),xlabel('time (min)'),ylabel('temp (c)'),text([1 3],[2 9],... 'critical point'),gtext('current point1'),grid,gtext('current point2') figure(2) semilogy(time,temp),xlabel('time (min)'),ylabel('temp (c)'),text([1 3],[2 9],... 'critical point'),gtext('current point1'),grid,gtext('current point2') Output: Command Window # time = 1 2 3 4 5 temp = 2 5 9 14 20 Graphics Window # Figure No. 1:

    ! Wc

  • CEE15 Introduction to Computing for Civil Engineers !

    Figure No. 2:

    !" G&?!fi6ure!ca&!2e!sa1e'!as!.fig eDte&sio&E!7or!eDa$5le,!figure1.figE!!5.4. Histograms (SKIPPED) hist(x) # T9e!co$$a&'!5lots!a!9isto6ra$!*it9!R[!i&ter1als!for!t9e!'ata!store'!i&!1ector!xE!hist(x,n) # T9e!co$$a&'!5lots!a!9isto6ra$!*it9!n i&ter1als!*it9!'ata!store'!i&!1ector!xE!hist(x,y) # Nt!5lots!a!9isto6ra$!for! t9e!'ata!i&!1ector!x *it9!t9e!i&ter1als!'efi&e'!2?!t9e!1ector!y,!a!1ector!*it9!ele$e&ts!i&!asce&'i&6!or'erE!

    ! Wn

  • CEE15 Introduction to Computing for Civil Engineers !

    Program: x = [2 5 7 9 8 3 2 6 9 3 5 4 7 6 5 9 2 4 6 8 5 3 4 6 1 3] figure(1) hist(x),title('Scores in midterm') figure(2) hist(x,[1 2 4 6]) figure(3) hist(x,5) Output in Graphics Window: Figure No. 1:

    !Figure No. 2:

    Figure No. 3:

    5.5. Plotting Options 5.5.1. Multiple Plots T9ere!are!t9ree!*a?s!to!5lot!$ulti5le!cur1es!o&!t9e!sa$e!6ra59E!!" Nf!2ot9!x a&'!y are!$atrices!of!t9e!sa$e!siQe,!t9e!colu$&s!of!x!are!5lotte'!*it9!t9e!colu$&s!of!y!i&!

    case!of!plot(x,y)E!

    ! We

  • CEE15 Introduction to Computing for Civil Engineers !

    !" (e!ca&!5lot!$ulti5le!cur1es!o&!t9e!sa$e!6ra59!2?!usi&6!$ulti5le!ar6u$e&ts!i&!t9e!5lot!co$$a&',!as!i&!t9e!follo*i&6:!!plot(x,y,w,z)!!

    #"

  • CEE15 Introduction to Computing for Civil Engineers !

    figure(1) plot(time,temp,':+g'),legend('timetemp1','timetemp2'),xlabel('time'),... ylabel('temp') figure(2) plot(x,y,':g',a,b,'-.xb'),legend('xy','ab',2),title('multiple curves') hold plot(c,d,'-rO'),legend('xy','ab','cd',2) Output: Command Window: time = 1 0 2 2 3 4 4 6 5 8 temp = 2 12 5 15 9 18 14 32 20 57 x = 1 2 3 4 5 y = 2 5 11 17 26 a = 2 4 6 b = 12 17 19 c = 0 1 2 d = 7 11 42 Current plot held Graphics Window: Figure No. 1:

    Figure No. 2:

    ! ZR

  • CEE15 Introduction to Computing for Civil Engineers !

    5.5.2. Sub Plots T9e! subplot co$$a&'! allo*s! us! to! s5lit! t9e! 6ra59ics! *i&'o*! i&to! su2*i&'o*sE! T9e! co$$a&'!subplot(m,n,p) s5lits! t9e! 6ra59ics! *i&'o*! i&to!m ro*s,! n colu$&s! a&'! sets! su2*i&'o*! p as! t9e!curre&tE!T9e!su2*i&'o*s!are!&u$2ere'!2?!ro*!fro$!left!to!ri69t,!to5!to!2otto$E!!Program: time = [1 2 3 4 5 6] temp = [13 16 20 29 40 48] x = [1 2 3 4 5] y = [2 5 11 17 26] a = [2 4 6] b = [12 17 19] c = [0 1 2] d = [7 11 42] subplot(2,2,1),plot(time,temp,':+g'),legend('timetemp'),... xlabel('time'),ylabel('temp') subplot(2,2,2),plot(x,y,':g',a,b,'-.xb'),legend('xy','ab',2),... title('multiple curves') subplot(2,2,3),plot(c,d,'-rO'),legend('cd',1) Output: Command Window : time = 1 2 3 4 5 6 temp = 13 16 20 29 40 48 x = 1 2 3 4 5 y = 2 5 11 17 26 a = 2 4 6 b = 12 17 19 c = 0 1 2 d = 7 11 42 Graphics Window:

    ! ZU

  • CEE15 Introduction to Computing for Civil Engineers !

    5.5.3. Special Symbols in Labels "o&si'er!t9e!follo*i&6!5ro6ra$:!clear!all_!clc_!close!all_!te$5\JRYER_!for!i\R:W[[!!!!!te$5\te$5`[ER_!!!!!DKiP\te$5_!!!!!if!a2sKDKiPPuWE[!!!!!!!!!?KiP\si6&KDKiPP_!!!!!else!!!!!!!!!?KiP\DKiPbWE[_!!!!!e&'!!!!!if!DKiPtJR!!!!!!!!!QKiP\JRE[JRbDKiP_!!!!!elseif!DKiPuR!!!!!!!!!QKiP\RJRbDKiP_!!!!!else!!!!!!!!!QKiP\[E[_!!!!!e&'!!!!!*KiP\cot9KDKiPPJRbDKiP_!e&'!5lotKD,?,!{r{P_!9ol'!o&!5lotKD,Q,{2{,{>i&e(i't9{,WP_!9ol'!o&!5lotKD,*,{EJ6{P!9\le6e&'K{Gs?$5totic{,{"ritical{,{r5ti$al{,UP_!Dla2elK{gal59a{P_!?la2elK{gDi{P_!T9e!out5ut!*ill!2e!Ki&!6ra59ic!*i&'o*P:!

    ! ZW

  • CEE15 Introduction to Computing for Civil Engineers !

    -15 -10 -5 0 5 10 15-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    2

    3

    AsymptoticCriticalOptimal

    !+ore!c9aracter!seIue&ces!for!la2els!are!liste'!i&!ta2le!YEWE!

    Table 5.3 Character sequence for labels in plots Character sequence Symbol Character sequence Symbol Character sequence symbol

    gal59a! !! gu5silo&! "! gsi$! w!g2eta! #! g59i! 4 ! gle6! 5 !

    g6a$$a! $! gc9i! %! gi&ft?! & !g'elta! &! g5si! '! gclu2suit! (!ge5silo&! )! go$e6a! *! g'ia$o&'suit! +!gQeta! ,! gva$$a! -! g9eartsuit! .!geta! /! gAelta! 0! gs5a'esuit! 1!gt9eta! 2! gT9eta! 3! gleftri69tarro*! 4!

    g1art9eta! 6 ! g>a$2'a! 5! gleftarro*! 6!giota! 7! gOi! 8! gu5arro*! 9!g@a55a! :! gPi! ;! gri69tarro*! gsi6$a! H! geDists! < ! g2ullet! $"!g1arsi6$a! I! g&i! = ! g'i1! > !

    gtau! J! gco&6! ? ! g&eI! @ !geIui1! A ! ga55roD! B ! gale59! C !gN$! D ! gRe! E ! g*5! F!

    goti$es! G ! go5lus! H ! goslas9! I !gca5! J ! gcu5! K ! gsu5seteI! L !

    gsu5set! M ! gsu2seteI! N ! gsu2set! O !gi&t! P ! gi&! Q ! go! R !

    grfloor! K! glceil! L! g&a2la! S !

    ! ZZ

  • CEE15 Introduction to Computing for Civil Engineers !

    glfloor! M! gc'ot! }! gl'ots! ! !g5er5! T ! g&e6! U ! g5ri$e! ~!g*e'6e! V ! gti$es! ' ! g[! !grceil! N! gsur'! O! g$i'! y!g1ee! W ! g1ar5i! X ! gco5?ri69t! !

    gla&6le! Y ! gra&6le! Z ! ! ! 5.6. 3-D plots T9e!plot co$$a&'!for!t9e!UJA!*orl'!ca&!2e!eDte&'e'!i&to!WJA!*it9!plot3E!T9e!for$at!is!t9e!sa$e!as!t9e!UJA!5lot,!eDce5t!t9e!'ata!are!i&!tri5les!rat9er!t9a&!i&!5airsE!

    Program: x = [0 1 2 3 4 5 6 7 8 9]; y = [1 3 6 9 13 17 20 25 32 40]; height = [ 2 8 9 3 14 6 8 4 19 17]; plot3(x,y,height,'-xb'), title('Elevation above mean sea level'),... xlabel('x axis'),ylabel('yaxis'),zlabel('zaxis'),... legend('Height in meter'),grid Output: Command Window: x = 0 1 2 3 4 5 6 7 8 9 y = 1 3 6 9 13 17 20 25 32 40 height = 2 8 9 3 14 6 8 4 19 17 end of program Graphics Window:

    !5.7. Mesh Plots and Surface Plots (SKIPPED) G!$es9!surface!is!6e&erate'!2?!a!set!of!1alues!i&!a!$atriDE!Eac9!5oi&t!i&!t9e!$atriD!re5rese&ts!t9e!1alue!of!a!surface!t9at!corres5o&'s!to!t9e!5oi&t!i&!t9e!6ri'E!G!surface!5lot!loo@s!li@e!t9e!$es9!5lot!eDce5t!t9at!t9e!s5aces!2et*ee&!t9e!li&es!are!fille'!i&E!!Program: [xgrid,ygrid] = meshgrid(1:1:10,1:1:10); xgrid ygrid z = sqrt(abs(1-xgrid.^2-ygrid.^2)) mesh(z), title('mesh plot')

    ! ZY

  • CEE15 Introduction to Computing for Civil Engineers !

    Output: Command Window : xgrid = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 ygrid = 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 z = Columns 1 through 7 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 2.0000 2.6458 3.4641 4.3589 5.2915 6.2450 7.2111 3.0000 3.4641 4.1231 4.8990 5.7446 6.6332 7.5498 4.0000 4.3589 4.8990 5.5678 6.3246 7.1414 8.0000 5.0000 5.2915 5.7446 6.3246 7.0000 7.7460 8.5440 6.0000 6.2450 6.6332 7.1414 7.7460 8.4261 9.1652 7.0000 7.2111 7.5498 8.0000 8.5440 9.1652 9.8489 8.0000 8.1854 8.4853 8.8882 9.3808 9.9499 10.5830 9.0000 9.1652 9.4340 9.7980 10.2470 10.7703 11.3578 10.0000 10.1489 10.3923 10.7238 11.1355 11.6190 12.1655 Columns 8 through 10 8.0000 9.0000 10.0000 8.1854 9.1652 10.1489 8.4853 9.4340 10.3923 8.8882 9.7980 10.7238 9.3808 10.2470 11.1355 9.9499 10.7703 11.6190 10.5830 11.3578 12.1655 11.2694 12.0000 12.7671 12.0000 12.6886 13.4164 12.7671 13.4164 14.1067 Graphics window:

    ! ZX

  • CEE15 Introduction to Computing for Civil Engineers !

    Program: z = [1 4 6;5 7 13;12 15 23] mesh(z) Output: Command Window : z = 1 4 6 5 7 13 12 15 23 Graphics Window:

    Program: [xgrid,ygrid] = meshgrid(1:1:10,1:1:10); z = sqrt(abs(1-xgrid.^2-ygrid.^2)); surf(z), title('surface plot') Output: Graphics Window:

    ! Zc

  • CEE15 Introduction to Computing for Civil Engineers !

    5.8. Contour Plots (SKIPPED) "o&tour!5lots!s9o*!li&es!of!co&sta&t!ele1atio&!or!9ei69tE!

    !" T9e!co$$a&'!contour(z) 'ra*s!a!co&tour!5lot!of!$atriD!zE!T9e!&u$2er!of!co&tour!li&es!a&'!t9eir!1alues!are!c9ose&!auto$aticall?!2?!+GT>GHE!

    !" T9e!co$$a&'!contour(z,n) 5ro'uces!a!co&tour!5lot!of!$atriD!z *it9!n!co&tour!le1elsE!!" T9e! co$$a&'! contour(z,v) 5ro'uces! a! co&tour! 5lot! of! z *it9! co&tour! le1els! at! t9e! 1alues!

    s5ecifie'!2?!1ector!vE!!" T9e! co$$a&'!pcolor(z) fu&ctio&!$a5s! t9e! 9ei69t! to! a! set! of! colors! a&'! 5rese&ts! t9e! sa$e!

    i&for$atio&!as!t9e!contour(z)!5lotE!!" T9e!co$$a&'!contour3(z) *ill!6i1e!us!WJA!co&tour!5lotsE!

    !Program: [xgrid,ygrid] = meshgrid(-0.5:0.1:0.5,-0.5:0.1:0.5); z = sqrt(abs(1-xgrid.^2-ygrid.^2)); figure(1) contour(z),title('Contour plot with default number of control levels') figure(2) contour(z,10),title('Contour plot with 10 control levels') figure(3) contour(z,[0.8 0.85 0.9]),title('Contour plot with 3 specified interval') figure(4) pcolor(z),title('Contour plot with color mapping') Output: Figure No. 1:

    !Figure No. 2:

    ! Zn

  • CEE15 Introduction to Computing for Civil Engineers !

    Figure No. 3:

    Figure No. 4:!

    ! 5.9. Polynomial Regression 5.9.1 Polyfit Function T9e!+GT>GH! fu&ctio&! for! co$5uti&6! t9e!2est! fit! to! a! set!of!'ata!*it9!5ol?&o$ial!*it9! a! s5ecifie'!'e6ree!is!t9e!polyfit fu&ctio&E!T9is!fu&ctio&!9as!t9ree!ar6u$e&ts:!!T9e!x!a&'!y!coor'i&ates!of!t9e!'ata!5oi&ts!a&'!'e6ree!N of!t9e!5ol?&o$ialE!

  • CEE15 Introduction to Computing for Civil Engineers !

    Program: x = [1 2 3 4 5 6 7 8 9] y = [2 4 7 11 16 23 32 45 60] coef = polyfit(x,y,2) ybest = polyval(coef,x) z = [1 -2 -3 10] root = roots(z) Output: x = 1 2 3 4 5 6 7 8 9 y = 2 4 7 11 16 23 32 45 60 coef = 0.9405 -2.4548 4.7143 ybest = Columns 1 through 7 3.2000 3.5667 5.8143 9.9429 15.9524 23.8429 33.6143 Columns 8 through 9 45.2667 58.8000 z = 1 -2 -3 10 root = -2.0000 2.0000 + 1.0000i 2.0000 - 1.0000i

    ! Y[

  • CEE15 Introduction to Computing for Civil Engineers !

    Chapter 6 M-File Functions

    function output arguments = name of the function (input arguments) T9e! li&e! a2o1e! 'efi&es! t9e! i&5ut! a&'! out5ut! ar6u$e&ts! a&'! 'isti&6uis9es! t9e! fu&ctio&! file! fro$! t9e!scri5t!fileE!

    !" T9e!first!fe*!li&es!s9oul'!2e!co$$e&ts!2ecause!t9e?!*ill!2e!'is5la?e'!if!9el5!is!reIueste'!for!t9e!fu&ctio&!&a$e,!for!eDa$5le,!help circle K*9ere!circle!is!t9e!fu&ctio&!&a$ePE!

    !" T9e!o&l?!i&for$atio&!retur&e'!fro$!t9e!fu&ctio&!is!co&tai&e'!i&!t9e!out5ut!ar6u$e&tsE!Gl*a?s!c9ec@! to! 2e! sure! t9at! t9e! fu&ctio&! i&clu'es! a! state$e&t! t9at! assi6&s! a! 1alue! to! t9e! out5ut!ar6u$e&tE!

    !" T9e! sa$e! i&5ut! a&'! out5ut! ar6u$e&ts! or! $atrices! ca&! 2e! use'! i&! 2ot9! a! fu&ctio&! a&'! t9e!5ro6ra$!t9at!refere&ces!it,!alt9ou69!*e!ca&!use!se5arate!i&5ut!ar6u$e&tsE!G&?!1alues!co$5ute'!i&!t9e!fu&ctio&,!ot9er!t9a&!t9e!out5ut!ar6u$e&ts,!are!&ot!accessi2le!fro$!t9e!5ro6ra$E!

    !" G!fu&ctio&!t9at!is!6oi&6!to!retur&!$ore!t9a&!o&e!1alue!s9oul'!s9o*!all!1alues!to!2e!retur&e'!as!a!1ector!i&!t9e!fu&ctio&!state$e&t,!as!i&!t9is!eDa$5le,!*9ic9!*ill!retur&!t9ree!1alues:!function [dist, vel, accel] = motion(x)

    !" G!fu&ctio&!t9at!9as!$ulti5le!i&5ut!ar6u$e&ts!$ust!list!t9e!ar6u$e&ts!i&!t9e!fu&ctio&!state$e&t,!as!s9o*&!i&!t9e!eDa$5le,!*9ic9!9as!t*o!i&5ut!ar6u$e&ts:

    function error = mse(w,d) !" T9e!s5ecial!1aria2les!nargin a&'!nargout ca&!2e!use'!to!'eter$i&e!t9e!&u$2er!of!i&5ut!a&'!

    out5ut!ar6u$e&ts!res5ecti1el?E!

    Case 1: (single input and single output) Program: r = [0 1 2] volume = vol(r); fprintf('The volumes of the spheres are = ') disp(volume) Function (vol.m): function volume = vol(r) %This function calculates the volume of spheres volume = 4/3*pi*r.^3 Output: r = 0 1 2 volume = 0 4.1888 33.5103 The volumes of the spheres are = 0 4.1888 33.5103 Case 2: (multiple inputs and single output) Program: r = [0 1 2]; h = [1 3 5]; volume = vol(r,h); fprintf('The volumes of the spheres are = ') disp(volume) Function (vol.m): function volume = vol(r,h) %This function calculates the volume of cylinders

    ! YR

  • CEE15 Introduction to Computing for Civil Engineers !

    volume = pi*r.^2.*h Output: volume = 0 9.4248 62.8319 The volumes of the spheres are = 0 9.4248 62.8319 Case 3: (single input and multiple outputs) Program: r = [0 1 2]; [sarea,volume] = survol(r); fprintf('The volumes of the spheres are = ') disp(volume) fprintf('The surface areas of the spheres are = ') disp(sarea) Function (survol.m): function [sarea,volume] = survol(r) %This function calculates the volume and surface area of spheres volume = 4/3*pi*r.^3 sarea = 4*pi*r.^2 Output: volume = 0 4.1888 33.5103 sarea = 0 12.5664 50.2655 The volumes of the spheres are = 0 4.1888 33.5103 The surface areas of the spheres are = 0 12.5664 50.2655 Case 4: (multiple inputs and multiple outputs) Program: r = [3 1 2]; h = [2 4 6]; [volcylinder,volsphere] = vol(r,h); fprintf('The volumes of the spheres are = ') disp(volsphere) fprintf('The volumes of the cylinders are = ') disp(volcylinder) Function (vol.m): function [volcylinder,volsphere] = vol(r,h) %This function calculates the volume and surface area of spheres volsphere = 4/3*pi*r.^3 volcylinder = pi*r.^2.*h Output: volsphere = 113.0973 4.1888 33.5103 volcylinder = 56.5487 12.5664 75.3982 The volumes of the spheres are = 113.0973 4.1888 33.5103 The volumes of the cylinders are = 56.5487 12.5664 75.3982

    Chapter 7 Input/Output Functions

    7.1. Opening the Files fid = fopen(filename,permission) *9ere!5er$issio&s!are:!

    ! YU

  • CEE15 Introduction to Computing for Civil Engineers !

    {r{!!!!!!!!r5e&!file!for!rea'i&6!K'efaultPE!{*{!!!!!!r5e&!file,!or!create!&e*!file,!for!*riti&6_!'iscar'!eDisti&6!co&te&ts,!if!a&?E!{a{!!!!!!!r5e&!file,!or!create!&e*!file,!for!*riti&6_!a55e&'!'ata!to!t9e!e&'!of!t9e!fileE!{r`{!!!!!r5e&!file!for!rea'i&6!a&'!*riti&6E!{*`{!!!!r5e&!file,!or!create!a!&e*!file,!for!rea'i&6!a&'!*riti&6_!'iscar'!eDisti&6!co&te&ts,!if!a&?E!{a`{!!!!!r5e&!file,!or!create!&e*!file,!for!rea'i&6!a&'!*riti&6_!a55e&'!'ata!to!t9e!e&'!of!t9e!fileE!!Example: fid = fopen('exp.txt','w'); 7.2. Closing the Files fclose(fid) or fclose('all') 7.3. Writing the Output Files To!*rite!for$atte'!'ata!to!a!file,!*e!ca&!use!t9e!follo*i&6!co$$a&':!fprintf(fid,format,A,...) !*9ere! fid! is! t9e! file! i'e&tificatio&! use'! i&! t9e! o5e&! co$$a&',! format! is! t9e! 'escri5tor! fiel'! a&'!A!'e&otes!t9e!out5ut!ar6u$e&tsE!!7.3.1. Useful Switches e!!!!!!!ED5o&e&tial!&otatio&!Kusi&6!a!lo*ercase!e!as!i&!WERZRYe`[[P!E!!!!!!ED5o&e&tial!&otatio&!Kusi&6!a&!u55ercase!E!as!i&!WERZRYE`[[P!f!!!!!!!7iDe'J5oi&t!&otatio&!6!!!!!!T9e!$ore!co$5act!of!e!or!f,!as!'efi&e'!i&!]U^E!N&si6&ifica&t!Qeros!'o!&ot!5ri&tE!s!!!!!!!Btri&6!of!c9aracters!!g&!!!!!!!!Me*!li&e!gt!!!!!!!!!

  • CEE15 Introduction to Computing for Civil Engineers !

    Output:!A unit circle has circumference 6.283186. !Example: B = [8.8 7.7; 8800 7700] L = fopen('exp.txt','w'); fprintf(L,'X is %6.2f meters or %8.3f mm\n',9.9,9900,B) fclose(L) Output:!X is 9.90 meters or 9900.000 mm X is 8.80 meters or 8800.000 mm X is 7.70 meters or 7700.000 mm 7.4. Reading the Input Files A = fscanf(fid,format,size) (9ere!size!is!a&!ar6u$e&t!t9at!'eter$i&es!9o*!$uc9!'ata!is!rea'E!jali'!o5tio&s!are:!&!!!!!!!!Rea'!&!ele$e&ts!i&to!a!colu$&!1ectorE!N&f!!!!!Rea'!to!t9e!e&'!of!t9e!file,!resulti&6!i&!a!colu$&!1ector!co&tai&i&6!t9e!sa$e!&u$2er!of!ele$e&ts!

    as!are!i&!t9e!fileE!]$,&^!Rea'!e&ou69!ele$e&ts!to!fill!a&!$J2?J&!$atriD,!filli&6!t9e!$atriD!i&!colu$&!or'erE!&!ca&!2e!N&f,!

    2ut!&ot!$E!!Example: G&!GB"NN!teDt!file!calle'!eD5EtDt!loo@s!li@e:!0.00 1.00000000 0.10 1.10517092 ... 1.00 2.71828183 !+atla2!rea's!t9is!GB"NN!file!2ac@!i&to!a!t*oJcolu$&!+GT>GH!$atriD:!fid = fopen('exp.txt'); a = fscanf(fid,'%g %g',[2 inf]) % It has two rows now. a = a'; fclose(fid) !

    ! YZ

  • CEE15 Introduction to Computing for Civil Engineers !

    Appendix: Statistical Measurements:

    A.1. Simple Analysis!!" max(x) #!Nf!x is!a!1ector!t9e&!t9e!fu&ctio&!retur&s!t9e!lar6est!1alue!i&!xE!Nf!x is!a!$atriD!t9e&!t9e!

    fu&ctio&!retur&s!a!ro*!1ector!co&tai&i&6!t9e!$aDi$u$!ele$e&t!fro$!eac9!colu$&E!!" max(x,y) #!T9is!fu&ctio&!retur&s!a!$atriD!t9e!sa$e!siQe!as!x a&'!yE!Eac9!ele$e&t! i&! t9e!$atriD!

    co&tai&s! t9e!$aDi$u$!1alue! fro$! t9e!corres5o&'i&6!5ositio&s! i&!x a&'!yE!

  • CEE15 Introduction to Computing for Civil Engineers !

    !" prod(x,1) #! T9e! fu&ctio&! retur&s! a! ro*! 1ector! co&tai&i&6! t9e! 5ro'uct! of! ele$e&ts! fro$! eac9!colu$&E!

    !" prod(x,2) #!T9e!fu&ctio&!retur&s!a!colu$&!1ector!co&tai&i&6!t9e!5ro'uct!of!ele$e&ts!fro$!eac9!ro*E!

    !" cumsum(x) #! Nf!x is! a!$atriD,! t9is! fu&ctio&! retur&s! a!$atriD!of! t9e! sa$e! siQe! t9at! co&tai&s! t9e!cu$ulati1e!su$!of!1alues!i&!eac9!colu$&!till!t9at!5oi&t!i&!colu$&E!

    !" cumsum(x,1) #!Ba$e!as!cumsum(x)E!!" cumsum(x,2) #!Nf!x is!a!$atriD,!t9is!fu&ctio&!retur&s!a!$atriD!of!t9e!sa$e!siQe!t9at!co&tai&s!t9e!

    cu$ulati1e!su$!of!1alues!i&!eac9!ro*!till!t9at!5oi&t!i&!ro*E!!" cumprod(x) #! Nf!x is!a!$atriD,! t9is! fu&ctio&! retur&s!a!$atriD!of! t9e! sa$e!siQe! t9at!co&tai&s! t9e!

    cu$ulati1e!5ro'uct!of!1alues!i&!eac9!colu$&!till!t9at!5oi&t!i&!colu$&E!!" cumprod(x,1) #!Ba$e!as!cumprod(x)E!!" cumprod(x,2) #!Nf!x is!a!$atriD,!t9is!fu&ctio&!retur&s!a!$atriD!of!t9e!sa$e!siQe!t9at!co&tai&s!t9e!

    cu$ulati1e!5ro'uct!of!1alues!i&!eac9!ro*!till!t9at!5oi&t!i&!ro*E!!Program: x = [7 4 5 2] y = [7 4 5 2; 3 5 9 6] z = [1 2 3 4; 5 6 7 8] max_vector = max(x) max_matrix = max(y) max_yz = max(y,z) [a,b] = min(x) [c,d] = min(y) max1 = max(y,[],1) min2 = max(z,[],2) mean_vector = mean(x) mean_matrix = mean(y) mean1 = mean(y,1) mean2 = mean(z,2) med_vector = median(x) med_matrix = median(y) med1 = median(z,1) med2 = median(y,2) sum_vector = sum(x) prod_matrix = prod(y) cumprod_vector = cumprod(x) cumsum_matrix = cumsum(y) sumsum = sum(sum(x)) Output : x = 7 4 5 2 y = 7 4 5 2 3 5 9 6 z = 1 2 3 4 5 6 7 8 max_vector = 7 max_matrix = 7 5 9 6 max_yz = 7 4 5 4 5 6 9 8 a =

    ! YX

  • CEE15 Introduction to Computing for Civil Engineers !

    2 b = 4 c = 3 4 5 2 d = 2 1 1 1 max1 = 7 5 9 6 min2 = 4 8 mean_vector = 4.5000 mean_matrix = 5.0000 4.5000 7.0000 4.0000 mean1 = 5.0000 4.5000 7.0000 4.0000 mean2 = 2.5000 6.5000 med_vector = 4.5000 med_matrix = 5.0000 4.5000 7.0000 4.0000 med1 = 3 4 5 6 med2 = 4.5000 5.5000 sum_vector = 18 prod_matrix = 21 20 45 12 cumprod_vector = 7 28 140 280 cumsum_matrix = 7 4 5 2 10 9 14 8 sumsum = 18

    !" sort(x) # Nf!x is!a!1ector!t9e&!t9is!fu&ctio&!sorts!t9e!1alues!i&to!asce&'i&6!or'erE!Nf!x is!a!$atriD!t9e&!t9is!fu&ctio&!sorts!eac9!colu$&!i&to!asce&'i&6!or'erE!

    !" sort(x,1) #!T9is!fu&ctio&!retur&s!a!$atriD!of!siQe!x!*it9!sorti&6!eac9!colu$&!i&to!asce&'i&6!or'erE!!" sort(x,2) #!T9is!fu&ctio&!retur&s!a!$atriD!of!siQe!z!*it9!sorti&6!eac9!ro*!i&to!asce&'i&6!or'erE!!" [a, b] = sort(x) #!T9e!sorte'!1alues!are!retur&e'!to!t9e!$atriD!a a&'!reor'ere'!i&'ices!are!store'!i&!

    t9e!$atriD!bE!(e!ca&!also!use!t9e!follo*i&6!eD5ressio&,!! [a, b] = sort(x, 2),! i&! *9ic9! eac9! ro*! is! sorte'! i&to! asce&'i&6! or'er! a&'! store'! i&! a *it9! t9e!

    reor'ere'!i&'ices!2ei&6!store'!i&!bE!!

    Program: x = [7 4 5 2] y = [7 4 5 2; 3 5 9 6] z = [1 2 3 4; 8 6 7 5] sortx = sort(x) sorty = sort(y) sort1 = sort(y,1) sort2 = sort(z,2) [a,b] = sort(y) [c,d] = sort(z,2)

    ! Yc

  • CEE15 Introduction to Computing for Civil Engineers !

    !Output: x = 7 4 5 2 y = 7 4 5 2 3 5 9 6 z = 1 2 3 4 8 6 7 5 sortx = 2 4 5 7 sorty = 3 4 5 2 7 5 9 6 sort1 = 3 4 5 2 7 5 9 6 sort2 = 1 2 3 4 5 6 7 8 a = 3 4 5 2 7 5 9 6 b = 2 1 1 1 1 2 2 2 c = 1 2 3 4 5 6 7 8 d = 1 2 3 4 4 2 3 1

    A.2. Variance and Standard Deviation

    [ \UR

    n

    kk

    x

    n

    ]^ /

    %/0

  • CEE15 Introduction to Computing for Civil Engineers !

    ! Ye

    !" var(x) a&'!var(x,1) co$5ute!t9e!sa$e!t9i&6!as!sta&'ar'!'e1iatio&!2ut!all!t9e!1alues!are!sIuare'E!!" var(x,w)! co$5utes! t9e! 1aria&ce! usi&6! t9e!*ei69t! 1ector,!wE! T9e! &u$2er! of! ele$e&ts! i&!w!$ust!

    eIual!t9e!&u$2er!of!ro*s!i&!xE! Program: x = [7 4 5 2] y = [7 4 5 2; 3 5 9 6] w = [1 2] std_vector = std(x) std_matrix = std(y) std_n = std(y,1) std_n2 = std(y,1,2) varrw = var(y,w) Output: x = 7 4 5 2 y = 7 4 5 2 3 5 9 6 w = 1 2 std_vector = 2.0817 std_matrix = 2.8284 0.7071 2.8284 2.8284 std_n = 2.0000 0.5000 2.0000 2.0000 std_n2 = 1.8028 2.1651

    varrw = 3.5556 0.2222 3.5556 3.5556