SlideShare a Scribd company logo
1 of 13
Numerical analysis in
MATLAB
Rules of selecting variables in MATLAB
1. Variable name in MATLAB must start with a letter and can be up to 31
characters long. The trailing characters can be numbers, letters or
underscores. Many choices are forbidden as variable names, some for
very obvious reasons (such as a*b which signifies a multiplication of the
variables a and b).
2. Variable names in MATLAB are case sensitive, so that a and A are two
different objects.
3. It is good programming practise to employ meaningful variable names. In
our initial examples we have only used very simple (but appropriate)
names: however as the examples become more complex our variable
names will be more informative.
4. Variables names should not coincide with a predefined MATLAB
command or with any user-defined subroutines. To see whether a
variable name is already in use we can use the command type variable
name, but it may be better to use the command which variable name.
Precedence: The Order in Which Calculations
Are Performed
• Evaluate the MATLAB expressions
• Use MATLAB to calculate the expression
where a = 3, b = 5 and c = −3.
Mathematical Functions
• Trigonometric functions sin (sine), cos (cosine) and tan (tangent) with their
inverses being obtained by appending an a as in asin, acos or atan. Exponential
functions exp, log, log10 and ˆ.
Other functions There are a variety of other functions available in MATLAB that are
not so commonly used, but which will definitely be useful:
• round(x) Rounds a number to the nearest integer
• ceil(x) Rounds a number up to the nearest integer
• floor(x) Rounds a number down to the nearest integer
• fix(x) Rounds a number to the nearest integer towards zero
• rem(x,y) The remainder left after division
• mod(x,y) The signed remainder left after division
• abs(x) The absolute value of x
• sign(x) The sign of x
• factor(x) The prime factors of x
Format: The Way in Which Numbers
Appear
• Consider the following code
• Evaluate in MATLAB
Vectors in MATLAB
• One of the most powerful aspects of MATLAB is its use of vectors (and
ultimately matrices) as objects.
• We shall start with simple objects and construct these using the colon
symbol:
Output: [1 2 3 4 5]
We can measure the size of this matrix by typing size(r) whose output is
[1 5], which means one row and five columns.
Cont..
• This simple way of constructing a vector r = a:b creates a vector r
which runs from a to b in steps of one. We can change the step by
using the slightly more involved syntax r = a:h:b, which creates the
vector r running from a to b in steps of h, for instance
gives r = [1 3 5] and s = [1 1.5 2 2.5 3 3.5].
linspace
• Here s is set up as a row vector which runs from zero to one and has
one hundred elements and t again runs from zero to one but now has
ten elements.
• Note here that to set up a vector which runs from zero to one in steps
of 1/N, we can use w = 0:1/N:1 or W = linspace(0,1,N+1).
• For example trying typing s=0:0.1:1.0; length(s). You will find that s
has eleven elements!).
Manipulating Vectors and Dot Arithmetic
• We can achieve this in MATLAB by using dot arithmetic as
follows
>> a = [1 2 3];
>> b = [4 5 6];
>> a.*b
ans = 4 10 18
Setting Up Mathematical Functions
• Set up a vector x which contains the values from zero to one in steps
of one tenth. This can be done in a variety of ways:
• % Firstly just list all the values:
• x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0];
• % Use the colon construction
• x = 0:0.1:1.0;
• % Or use the command linspace
• x = linspace(0,1,11);
Example: Construct the polynomial y = (x+2)2(x3 +1) for values
of x from minus one to one in steps of 0.1.
In the construction of g we have used the dot arithmetic to
cube each element and then add one to it. When
constructing y we firstly square each element of f (with f.ˆ2)
and then multiply each element of this by the corresponding
element of g.
Examples
Some MATLAB Specific Commands
• The first command is polyval. This command takes two inputs, namely the
coefficients of a polynomial and the values at which you want to evaluate
it.
• % coefficient of the highest power first
• c = [1 3 -1 -1];
• % Now perform the evaluation using polyval
• y = polyval(c,x)

More Related Content

Similar to 2. Chap 1.pptx

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 beyondMahuaPal6
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsRay Phan
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlabTUOS-Sam
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABRavikiran A
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlabkrajeshk1980
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsMukesh Kumar
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersMurshida ck
 
Basic MATLAB-Presentation.pptx
Basic MATLAB-Presentation.pptxBasic MATLAB-Presentation.pptx
Basic MATLAB-Presentation.pptxPremanandS3
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2bilawalali74
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lecturesmarwaeng
 

Similar to 2. Chap 1.pptx (20)

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
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & Scientists
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Matlab pt1
Matlab pt1Matlab pt1
Matlab pt1
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlab
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Mbd dd
Mbd ddMbd dd
Mbd dd
 
MATLAB INTRODUCTION
MATLAB INTRODUCTIONMATLAB INTRODUCTION
MATLAB INTRODUCTION
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
EPE821_Lecture3.pptx
EPE821_Lecture3.pptxEPE821_Lecture3.pptx
EPE821_Lecture3.pptx
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Matlab booklet
Matlab bookletMatlab booklet
Matlab booklet
 
Basic MATLAB-Presentation.pptx
Basic MATLAB-Presentation.pptxBasic MATLAB-Presentation.pptx
Basic MATLAB-Presentation.pptx
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2
 
Matlab
MatlabMatlab
Matlab
 
Matlab for marketing people
Matlab for marketing peopleMatlab for marketing people
Matlab for marketing people
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lectures
 

More from HassanShah396906

lit-review-presentation [Autosaved].pptx
lit-review-presentation [Autosaved].pptxlit-review-presentation [Autosaved].pptx
lit-review-presentation [Autosaved].pptxHassanShah396906
 
Optoelectronics Device’s.pptx
Optoelectronics Device’s.pptxOptoelectronics Device’s.pptx
Optoelectronics Device’s.pptxHassanShah396906
 
5.Introduction_to_Heisenberg_model.pptx
5.Introduction_to_Heisenberg_model.pptx5.Introduction_to_Heisenberg_model.pptx
5.Introduction_to_Heisenberg_model.pptxHassanShah396906
 
M1 - Research Philosophy and Methods.pptx
M1 - Research Philosophy and Methods.pptxM1 - Research Philosophy and Methods.pptx
M1 - Research Philosophy and Methods.pptxHassanShah396906
 
Finite Element Method.pptx
Finite Element Method.pptxFinite Element Method.pptx
Finite Element Method.pptxHassanShah396906
 
Ch # 9 Electrode Polarization.pptx
Ch # 9 Electrode Polarization.pptxCh # 9 Electrode Polarization.pptx
Ch # 9 Electrode Polarization.pptxHassanShah396906
 
Density Functional Theory.pptx
Density Functional Theory.pptxDensity Functional Theory.pptx
Density Functional Theory.pptxHassanShah396906
 
Introduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptxIntroduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptxHassanShah396906
 
Molecular dynamics Simulation.pptx
Molecular dynamics Simulation.pptxMolecular dynamics Simulation.pptx
Molecular dynamics Simulation.pptxHassanShah396906
 

More from HassanShah396906 (13)

lit-review-presentation [Autosaved].pptx
lit-review-presentation [Autosaved].pptxlit-review-presentation [Autosaved].pptx
lit-review-presentation [Autosaved].pptx
 
Bone Cement.pptx
Bone Cement.pptxBone Cement.pptx
Bone Cement.pptx
 
Optoelectronics Device’s.pptx
Optoelectronics Device’s.pptxOptoelectronics Device’s.pptx
Optoelectronics Device’s.pptx
 
5.Introduction_to_Heisenberg_model.pptx
5.Introduction_to_Heisenberg_model.pptx5.Introduction_to_Heisenberg_model.pptx
5.Introduction_to_Heisenberg_model.pptx
 
M1 - Research Philosophy and Methods.pptx
M1 - Research Philosophy and Methods.pptxM1 - Research Philosophy and Methods.pptx
M1 - Research Philosophy and Methods.pptx
 
Finite Element Method.pptx
Finite Element Method.pptxFinite Element Method.pptx
Finite Element Method.pptx
 
Ch # 9 Electrode Polarization.pptx
Ch # 9 Electrode Polarization.pptxCh # 9 Electrode Polarization.pptx
Ch # 9 Electrode Polarization.pptx
 
Density Functional Theory.pptx
Density Functional Theory.pptxDensity Functional Theory.pptx
Density Functional Theory.pptx
 
5. Summing Series.pptx
5. Summing Series.pptx5. Summing Series.pptx
5. Summing Series.pptx
 
Introduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptxIntroduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptx
 
Molecular dynamics Simulation.pptx
Molecular dynamics Simulation.pptxMolecular dynamics Simulation.pptx
Molecular dynamics Simulation.pptx
 
Interconnects.pptx
Interconnects.pptxInterconnects.pptx
Interconnects.pptx
 
VMDL_FEATURES.ppt
VMDL_FEATURES.pptVMDL_FEATURES.ppt
VMDL_FEATURES.ppt
 

Recently uploaded

Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 sciencefloriejanemacaya1
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...RohitNehra6
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzohaibmir069
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRDelhi Call girls
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfSwapnil Therkar
 
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfWadeK3
 

Recently uploaded (20)

Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 science
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistan
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
 
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
 

2. Chap 1.pptx

  • 2. Rules of selecting variables in MATLAB 1. Variable name in MATLAB must start with a letter and can be up to 31 characters long. The trailing characters can be numbers, letters or underscores. Many choices are forbidden as variable names, some for very obvious reasons (such as a*b which signifies a multiplication of the variables a and b). 2. Variable names in MATLAB are case sensitive, so that a and A are two different objects. 3. It is good programming practise to employ meaningful variable names. In our initial examples we have only used very simple (but appropriate) names: however as the examples become more complex our variable names will be more informative. 4. Variables names should not coincide with a predefined MATLAB command or with any user-defined subroutines. To see whether a variable name is already in use we can use the command type variable name, but it may be better to use the command which variable name.
  • 3. Precedence: The Order in Which Calculations Are Performed • Evaluate the MATLAB expressions • Use MATLAB to calculate the expression where a = 3, b = 5 and c = −3.
  • 4. Mathematical Functions • Trigonometric functions sin (sine), cos (cosine) and tan (tangent) with their inverses being obtained by appending an a as in asin, acos or atan. Exponential functions exp, log, log10 and ˆ. Other functions There are a variety of other functions available in MATLAB that are not so commonly used, but which will definitely be useful: • round(x) Rounds a number to the nearest integer • ceil(x) Rounds a number up to the nearest integer • floor(x) Rounds a number down to the nearest integer • fix(x) Rounds a number to the nearest integer towards zero • rem(x,y) The remainder left after division • mod(x,y) The signed remainder left after division • abs(x) The absolute value of x • sign(x) The sign of x • factor(x) The prime factors of x
  • 5. Format: The Way in Which Numbers Appear • Consider the following code • Evaluate in MATLAB
  • 6. Vectors in MATLAB • One of the most powerful aspects of MATLAB is its use of vectors (and ultimately matrices) as objects. • We shall start with simple objects and construct these using the colon symbol: Output: [1 2 3 4 5] We can measure the size of this matrix by typing size(r) whose output is [1 5], which means one row and five columns.
  • 7. Cont.. • This simple way of constructing a vector r = a:b creates a vector r which runs from a to b in steps of one. We can change the step by using the slightly more involved syntax r = a:h:b, which creates the vector r running from a to b in steps of h, for instance gives r = [1 3 5] and s = [1 1.5 2 2.5 3 3.5].
  • 8. linspace • Here s is set up as a row vector which runs from zero to one and has one hundred elements and t again runs from zero to one but now has ten elements. • Note here that to set up a vector which runs from zero to one in steps of 1/N, we can use w = 0:1/N:1 or W = linspace(0,1,N+1). • For example trying typing s=0:0.1:1.0; length(s). You will find that s has eleven elements!).
  • 9. Manipulating Vectors and Dot Arithmetic • We can achieve this in MATLAB by using dot arithmetic as follows >> a = [1 2 3]; >> b = [4 5 6]; >> a.*b ans = 4 10 18
  • 10. Setting Up Mathematical Functions • Set up a vector x which contains the values from zero to one in steps of one tenth. This can be done in a variety of ways: • % Firstly just list all the values: • x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]; • % Use the colon construction • x = 0:0.1:1.0; • % Or use the command linspace • x = linspace(0,1,11);
  • 11. Example: Construct the polynomial y = (x+2)2(x3 +1) for values of x from minus one to one in steps of 0.1. In the construction of g we have used the dot arithmetic to cube each element and then add one to it. When constructing y we firstly square each element of f (with f.ˆ2) and then multiply each element of this by the corresponding element of g.
  • 13. Some MATLAB Specific Commands • The first command is polyval. This command takes two inputs, namely the coefficients of a polynomial and the values at which you want to evaluate it. • % coefficient of the highest power first • c = [1 3 -1 -1]; • % Now perform the evaluation using polyval • y = polyval(c,x)