SlideShare a Scribd company logo
1 of 14
Download to read offline
Matlab Notes Part 1
Austin Baird
UNC Department of Mathematics
UNC Department of Biology
January 15, 2014
Introduction
• Matlab is a lot like a calculator!
• we can compute things like 3 · 2
• Arithmetic is preformed in order of operations (just like a
calculator)
• Matrices and matrix operations are where Matlab excels
• Today we will:
• Understand the basic Desktop environment
• Preform basic arithmetic
• use built in functions
• Create matices and write our first code
Desktop Basics
Desktop Basics
• Current Folder: This is where the files you are working
with will be stored. Note: you can navigate from the folder
you’re in to any folder you desire by clicking the magnifying
glass.
• Command Window: This is where you can calculate,
receive error messages, or print output from your
programs. Commands are input at the symbol.
• try to calculate things right now!
• Workspace: This is where variables are stored and will tell
you things about them (what they equal, how big a matrix is
ect.)
• Command History: This is where the commands you’ve
issued most recently will appear. ↑ on your keyboard will
scroll through these commands.
• Refer to the text (Matlab intro) for more detailed instruction.
Troubleshooting
• Often times things won’t work.
• One Common problem is that a variable you thought was
free is actually associated with a value.
• Try the command clear. This will clear all the variables
in the Workspace.
Try and do some computing and assign
variables to values.
Arrays
• Matlab = Matrix Laboratory, so do things with matrices!
• Create your first matrix!
• a = [1234], Row Vector!
• Indexing in matlab begins with 1 (NOT 0)
• Add more rows: a = [1234; 2345; 3456]
• Note This also works: a = [1, 2, 3, 4; 2, 3, 4, 5; 3, 4, 5, 6]
• We can call values from this array:
• a(1, 2)
• 2
• Or assign a value to a particular position:
• a(1, 2) = 10
• The notation is (row, column)
Array Creating Functions
• There are ways to automatically create an array(matrix) in
matlab:
• a = zeros(3, 2)
• Creates an array of all zeros (this is good for data
management if you want a set size for your matrix
• a = linspace(0, 1, 100)
• This creates a vector which has 100 points filled in between
0 and 1.
• a = 0 : 0.1 : 1
• this creates an array which has mesh width equal to 0.1
• How do we model functions in Matlab?
Array Operations
• Colon operator:
• a = [123; 234; 456]
• The colon denotes start:end. Note: if you just place a
colon it will select everything.
• Select the first two rows of our matrix:
• a(1 : 2, :)
• What does a(1, :) do?
• We can also add to each element of the array:
• a − a
• a − 10
• We can do traditional and element wise multiplication of
matrices:
• traditional: a ∗ a, element wise: a. ∗ a
• In a similar way you can do other things element wise to a
matrix ex.: a. ∧ 3
Discrete Functions
• In math things are continuous, in Matlab (and in
computational science in general) This is not the case.
• To compute and evaluate functions we need a Domain and
their corresponding function values Range
• To get a Domain in Matlab we must “discretize” our
continuous domain.
• We do this by creating a “mesh width” = size of our spacing
in our discretization
• ex: x = linspace(0, 1, 10) what is our mesh width dx
(x(2)-x(1))? is it always equal? Must consider this!
• ex: x = 0 : 0.1 : 1 what is our mesh width dx?
Evaluating Functions
• Now that we have a discretized version of the x-axis we
can now evaluate a function!
• Matlab has many built in functions (sin, cos, tan, exp...)
• y = sin(x)
• We now have a set of range values, how do we know that
this is right? Graph?
• plot this function: plot(y)
• In general the resolution of our function is dependent upon
our mesh width
• How small is enough, how big is too big? Does any mesh
width work?
• A quick and dirty estimate of the error between two arrays:
max(a − b) will select the max error between the arrays
(infinity norm).
• Plot sin over one period and experiment with different
mesh widths and their effects
Our First Program
• Matlab files are denoted by a .m at the end of the name.
• Documentation is very very important! % is how you
comment lines (these will not be read by the computer)
• Notice that to run the code Matlab asks you whether to add
folder to path or to change to directory, both work.
• Just type the name of the code in the command line to get
the program to run.
How to Run
Output
Homework
• Read sections 1-17 to 1-23
• Email me by Tuesday at 12:01am a onyen.m file which
when run does the following ( and is commented at the top
of the file with your full name and who you worked with, if
anyone):
• Generates a figure which plots y = sin(x) + c for ten values
of c (all on the same figure).
• Generates the figure in section 1-23.

More Related Content

What's hot

Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in spaceFaizan Shabbir
 
Linear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialLinear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialJia-Bin Huang
 
Writing Fast MATLAB Code
Writing Fast MATLAB CodeWriting Fast MATLAB Code
Writing Fast MATLAB CodeJia-Bin Huang
 
Introduction to programming in MATLAB
Introduction to programming in MATLABIntroduction to programming in MATLAB
Introduction to programming in MATLABmustafa_92
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#Rizwan Ali
 
Introduction to programming using mat ab
Introduction to programming using mat abIntroduction to programming using mat ab
Introduction to programming using mat abAhmed Hisham
 
Alg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatricesAlg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatricesjtentinger
 
Alg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - MatricesAlg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - Matricesjtentinger
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operatorsLuckshay Batra
 
6nullables in c#
6nullables in c#6nullables in c#
6nullables in c#Sireesh K
 
9 Hierarchical Clustering
9 Hierarchical Clustering9 Hierarchical Clustering
9 Hierarchical ClusteringVishal Dutt
 

What's hot (19)

Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Linear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialLinear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorial
 
Writing Fast MATLAB Code
Writing Fast MATLAB CodeWriting Fast MATLAB Code
Writing Fast MATLAB Code
 
Introduction to programming in MATLAB
Introduction to programming in MATLABIntroduction to programming in MATLAB
Introduction to programming in MATLAB
 
Matlab vectors
Matlab vectorsMatlab vectors
Matlab vectors
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
 
Introduction to programming using mat ab
Introduction to programming using mat abIntroduction to programming using mat ab
Introduction to programming using mat ab
 
Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
 
Alg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatricesAlg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatrices
 
Alg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - MatricesAlg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - Matrices
 
matlab
matlabmatlab
matlab
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operators
 
6nullables in c#
6nullables in c#6nullables in c#
6nullables in c#
 
Matlab tutorial 1
Matlab tutorial 1Matlab tutorial 1
Matlab tutorial 1
 
Lec2
Lec2Lec2
Lec2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Logical vectors
Logical vectorsLogical vectors
Logical vectors
 
9 Hierarchical Clustering
9 Hierarchical Clustering9 Hierarchical Clustering
9 Hierarchical Clustering
 

Similar to Matlab pt1

Similar to Matlab pt1 (20)

Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
 
Introduction to Matlab.ppt
Introduction to Matlab.pptIntroduction to Matlab.ppt
Introduction to Matlab.ppt
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Mit6 094 iap10_lec01
Mit6 094 iap10_lec01Mit6 094 iap10_lec01
Mit6 094 iap10_lec01
 
EE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manualEE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manual
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Matlab guide
Matlab guideMatlab guide
Matlab guide
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 
INTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.pptINTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.ppt
 
MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptMATLAB_CIS601-03.ppt
MATLAB_CIS601-03.ppt
 
From zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondFrom zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyond
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Image processing
Image processingImage processing
Image processing
 
MATLAB INTRODUCTION
MATLAB INTRODUCTIONMATLAB INTRODUCTION
MATLAB INTRODUCTION
 
Matlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - IMatlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - I
 
Basic matlab for beginners
Basic matlab for beginnersBasic matlab for beginners
Basic matlab for beginners
 
MATLAB workshop lecture 1MATLAB work.ppt
MATLAB workshop lecture 1MATLAB work.pptMATLAB workshop lecture 1MATLAB work.ppt
MATLAB workshop lecture 1MATLAB work.ppt
 

Recently uploaded

Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 

Recently uploaded (20)

Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 

Matlab pt1

  • 1. Matlab Notes Part 1 Austin Baird UNC Department of Mathematics UNC Department of Biology January 15, 2014
  • 2. Introduction • Matlab is a lot like a calculator! • we can compute things like 3 · 2 • Arithmetic is preformed in order of operations (just like a calculator) • Matrices and matrix operations are where Matlab excels • Today we will: • Understand the basic Desktop environment • Preform basic arithmetic • use built in functions • Create matices and write our first code
  • 4. Desktop Basics • Current Folder: This is where the files you are working with will be stored. Note: you can navigate from the folder you’re in to any folder you desire by clicking the magnifying glass. • Command Window: This is where you can calculate, receive error messages, or print output from your programs. Commands are input at the symbol. • try to calculate things right now! • Workspace: This is where variables are stored and will tell you things about them (what they equal, how big a matrix is ect.) • Command History: This is where the commands you’ve issued most recently will appear. ↑ on your keyboard will scroll through these commands. • Refer to the text (Matlab intro) for more detailed instruction.
  • 5. Troubleshooting • Often times things won’t work. • One Common problem is that a variable you thought was free is actually associated with a value. • Try the command clear. This will clear all the variables in the Workspace. Try and do some computing and assign variables to values.
  • 6. Arrays • Matlab = Matrix Laboratory, so do things with matrices! • Create your first matrix! • a = [1234], Row Vector! • Indexing in matlab begins with 1 (NOT 0) • Add more rows: a = [1234; 2345; 3456] • Note This also works: a = [1, 2, 3, 4; 2, 3, 4, 5; 3, 4, 5, 6] • We can call values from this array: • a(1, 2) • 2 • Or assign a value to a particular position: • a(1, 2) = 10 • The notation is (row, column)
  • 7. Array Creating Functions • There are ways to automatically create an array(matrix) in matlab: • a = zeros(3, 2) • Creates an array of all zeros (this is good for data management if you want a set size for your matrix • a = linspace(0, 1, 100) • This creates a vector which has 100 points filled in between 0 and 1. • a = 0 : 0.1 : 1 • this creates an array which has mesh width equal to 0.1 • How do we model functions in Matlab?
  • 8. Array Operations • Colon operator: • a = [123; 234; 456] • The colon denotes start:end. Note: if you just place a colon it will select everything. • Select the first two rows of our matrix: • a(1 : 2, :) • What does a(1, :) do? • We can also add to each element of the array: • a − a • a − 10 • We can do traditional and element wise multiplication of matrices: • traditional: a ∗ a, element wise: a. ∗ a • In a similar way you can do other things element wise to a matrix ex.: a. ∧ 3
  • 9. Discrete Functions • In math things are continuous, in Matlab (and in computational science in general) This is not the case. • To compute and evaluate functions we need a Domain and their corresponding function values Range • To get a Domain in Matlab we must “discretize” our continuous domain. • We do this by creating a “mesh width” = size of our spacing in our discretization • ex: x = linspace(0, 1, 10) what is our mesh width dx (x(2)-x(1))? is it always equal? Must consider this! • ex: x = 0 : 0.1 : 1 what is our mesh width dx?
  • 10. Evaluating Functions • Now that we have a discretized version of the x-axis we can now evaluate a function! • Matlab has many built in functions (sin, cos, tan, exp...) • y = sin(x) • We now have a set of range values, how do we know that this is right? Graph? • plot this function: plot(y) • In general the resolution of our function is dependent upon our mesh width • How small is enough, how big is too big? Does any mesh width work? • A quick and dirty estimate of the error between two arrays: max(a − b) will select the max error between the arrays (infinity norm). • Plot sin over one period and experiment with different mesh widths and their effects
  • 11. Our First Program • Matlab files are denoted by a .m at the end of the name. • Documentation is very very important! % is how you comment lines (these will not be read by the computer) • Notice that to run the code Matlab asks you whether to add folder to path or to change to directory, both work. • Just type the name of the code in the command line to get the program to run.
  • 14. Homework • Read sections 1-17 to 1-23 • Email me by Tuesday at 12:01am a onyen.m file which when run does the following ( and is commented at the top of the file with your full name and who you worked with, if anyone): • Generates a figure which plots y = sin(x) + c for ten values of c (all on the same figure). • Generates the figure in section 1-23.