Top Banner
MATLAB Examples Hans-Petter Halvorsen, M.Sc. Scripts and User-defined Functions
12

MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

May 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

MATLABExamples

Hans-PetterHalvorsen,M.Sc.

ScriptsandUser-definedFunctions

Page 2: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

Scripts(m-files)ScriptEditor WhenusingtheScriptEditor,youmaycreateseverallinesofcodeandexecute

allinonebatch.Youcaneasilydochangesinyourcode,createcomments,etc.

RuntheScript

clearclc

x=0:0.1:2*pi;y=sin(x);y2=cos(x);y3=tan(x);y4=atan(x);

%plotting sin(x)subplot(2,2,1)plot(x,y)

%plotting cos(x)subplot(2,2,2)plot(x,y2)

%plotting tan(x)subplot(2,2,3)plot(x,y3)

%plotting atan(x)subplot(2,2,4)plot(x,y4)

MATLABScriptsaresavedasso-called.mfiles(fileextensionis.m)

Page 3: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

Script(M-file)

• CreateaScript(M-file)whereyoucreateavectorwithrandomdataandfindtheaverageandthestandarddeviation

• RuntheScriptfromtheCommandwindow.

Page 4: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

x = rand(10, 1)

mean(x)

std(x)

x = 0.8147 0.9058 0.1270 0.9134 0.6324 0.0975 0.2785 0.5469 0.9575 0.9649

ans = 0.6239

ans = 0.3459

Page 5: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you
Page 6: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

User-definedFunctions

YouCreatetheFunctionintheEditor

MATLABcontainshundredsofbuilt-infunctions,butveryoftenyouneedtocreateyourownfunctions

YouUsetheFunctionintheCommandWindoworinaScript

Input

Returnvalue

function output = function_name(input)

Page 7: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

User-definedfunction

• Createafunctioncalc_average() thatfindstheaverageoftwonumbers.

• TestthefunctionafterwardsintheCommandwindowasfollows:>>x=2;>>y=4;>>z=calc_average(x,y)

Page 8: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

>> z=calc_average(x,y)z =

3

WetestthefunctionintheCommandwindow

Page 9: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

User-definedfunctionII

• Createafunctioncircle thatfindstheareainacirclebasedontheinputparameterr(radius).

• RunandtestthefunctionintheCommandwindow.

𝐴 = 𝜋𝑟&

𝑟

Page 10: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

function A = circle(r)

A=pi*r*r;

Wedefinethefunction:

TestingthefunctionfromtheCommandwindow:

>> circle(1)ans =

3.1416>> circle(2)ans =

12.5664

>> r=4;>> A=circle(r)A =

50.2655

Page 11: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you
Page 12: MATLAB Examples - Scripts and User-defined Functions...MATLAB Scripts are saved as so-called .m files (file extension is .m) Script (M-file) • Create a Script (M-file) where you

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:[email protected]:http://home.hit.no/~hansha/