SlideShare a Scribd company logo
1 of 11
Chapter 3: Summing Series
Summing Series
𝑖=1
𝑁
𝑖2
N = input(’Enter the number of terms required: ’);
s = 0;
for i = 1:N
s = s + iˆ2;
end
disp([’Sum of the first ’ int2str(N) ‘ squares is ’ int2str(s)])
• At the moment we will stick to f(i) = iˆ2. Again we shall start by doing an example
by hand, let us determine the values of I_N for N equals one to four. Firstly N = 1
𝐼1 =
𝑖=1
𝑁
𝑖2 = 1
For N=2
𝐼2 =
𝑖=1
𝑁
𝑖2 = 1 + 4 = 5
For N=3
𝐼3 =
𝑖=1
𝑁
𝑖2
= 1 + 4 + 9 = 14
In general,
𝐼𝑁 = 𝐼 (𝑁−1) + 𝑁2
maxN = input(’Enter the maximum value of N required: ’);
I(1) = 1ˆ2;
for N = 2:maxN
I(N) = I(N-1) + Nˆ2;
end
disp([’Values of I_N’])
disp([1:N; I])
Summing Infinite Series
• The Taylor series for a function f(x) about a point x = a is given by
• We can use this to approximate sin x by an infinite series in x as
• Of course in using a computer we cannot actually take N to be infinity, but we
shall take it to be large in the hope that the error will be small. We can write the
above series (taking N = 10) and evaluate the series at the points 0, 0.1, 0.2, 0.3,
0.4 and 0.5 using
v = 0.0:0.1:0.5;
sinx = zeros(size(v));
N = 10; range = 0:N;
ints = 2*range+1;
for n = range
sinx = sinx+(-1)ˆn*v.ˆints(n+1)/(factorial(ints(n+1)));
end
v = 0.0:0.1:0.5; sets up the vector v running from zero to a half in steps of a tenth.
sinx = zeros(size(v)); The command size(v) gives the size of the vector v and then the
command zero sets up sinx as an array of zeros of the same size.
N = 10; range = 0:N; sets a variable N to be equal to 10 and then sets up a vector range
which runs from zero to N.
ints = 2*range+1; This gives the mathematical expression 2n+1 for all the elements of the
vector range and puts them in ints.
for n = range ..... end This loop structure repeats its arguments for n equal to all the
elements of range.
sinx = sinx+(-1)ˆn*v.ˆints(n+1)/(factorial(ints(n+1))); This mathematical expression evaluates
the subject of the summation as a function of n. ints(1) refers to the value when n = 0). Also
note the use of .ˆ when operating on v as this is a vector.
IF, elseif, else statements
• It is convenient at this stage to introduce some of the other commands which are
available to us when constructing conditional statements, namely else and elseif.
The general form of these is given by:
if (expression)
commands ...
elseif (expression)
commands ...
else
commands ...
End
• Notice that each if statement must be paired with an end statement, but we can
use as many elseif statements as we like, but only one else (within a certain
level). We are also able to nest if statements: again it is recommended that the
new levels are indented so as to aid readability.
Example 3.12 Consider the following piece of code which determines
which numbers between 2 and 9 go into a specified integer exactly:
str = ’Divisible by ’;
x = input(’Number to test: ’);
for j = 2:9
if rem(x,j) == 0
disp([str int2str(j)])
end
end
Example 3.13 Here we construct a conditional statement which
evaluates the function:
if x >= 0 & x <= 1
f = x;
elseif x > 1 & x <= 2
f = 2-x;
else
f = 0;
end
Conditional loops
• Suppose we now want to repeat a loop until a certain condition is
satisfied. This is achieved by making use of the MATLAB command
while, which has the syntax
while (condition)
commands...
end
• Example 3.16: Write out the values of x2 for all positive integer values x such that
x3 < 2000. To do this we will use the code
Iteration
• repetition of a mathematical or computational procedure applied
to the result of a previous application, typically as a means of
obtaining successively closer approximations to the solution of
a problem.

More Related Content

Similar to 5. Summing Series.pptx

Explanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expertExplanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expert홍배 김
 
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier AnalysisDSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier AnalysisAmr E. Mohamed
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questionsGradeup
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Ziyauddin Shaik
 
Ee693 sept2014quizgt2
Ee693 sept2014quizgt2Ee693 sept2014quizgt2
Ee693 sept2014quizgt2Gopi Saiteja
 
Theoryofcomp science
Theoryofcomp scienceTheoryofcomp science
Theoryofcomp scienceRaghu nath
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Problem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfProblem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfJUSTSTYLISH3B2MOHALI
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpExcel Homework Help
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
Informe laboratorio n°1
Informe laboratorio n°1Informe laboratorio n°1
Informe laboratorio n°1luisescobedo38
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2docSrikanth
 
Dynamical systems solved ex
Dynamical systems solved exDynamical systems solved ex
Dynamical systems solved exMaths Tutoring
 

Similar to 5. Summing Series.pptx (20)

Explanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expertExplanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expert
 
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier AnalysisDSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
DSP_FOEHU - MATLAB 02 - The Discrete-time Fourier Analysis
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 
White box-sol
White box-solWhite box-sol
White box-sol
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
Ee693 sept2014quizgt2
Ee693 sept2014quizgt2Ee693 sept2014quizgt2
Ee693 sept2014quizgt2
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
L06
L06L06
L06
 
Montecarlophd
MontecarlophdMontecarlophd
Montecarlophd
 
Theoryofcomp science
Theoryofcomp scienceTheoryofcomp science
Theoryofcomp science
 
big_oh
big_ohbig_oh
big_oh
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Problem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfProblem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdf
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
algorithm
algorithmalgorithm
algorithm
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
Informe laboratorio n°1
Informe laboratorio n°1Informe laboratorio n°1
Informe laboratorio n°1
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Dynamical systems solved ex
Dynamical systems solved exDynamical systems solved ex
Dynamical systems solved ex
 

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
 
2. Chap 1.pptx
2. Chap 1.pptx2. Chap 1.pptx
2. Chap 1.pptx
 
Density Functional Theory.pptx
Density Functional Theory.pptxDensity Functional Theory.pptx
Density Functional Theory.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

Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxEran Akiva Sinbar
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Masticationvidulajaib
 
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
 
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |aasikanpl
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantadityabhardwaj282
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxpriyankatabhane
 
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
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsssuserddc89b
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫qfactory1
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxFarihaAbdulRasheed
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2John Carlo Rollon
 
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
 

Recently uploaded (20)

Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
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
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Mastication
 
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
 
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are important
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptx
 
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
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physics
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2
 
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🔝
 

5. Summing Series.pptx

  • 2. Summing Series 𝑖=1 𝑁 𝑖2 N = input(’Enter the number of terms required: ’); s = 0; for i = 1:N s = s + iˆ2; end disp([’Sum of the first ’ int2str(N) ‘ squares is ’ int2str(s)])
  • 3. • At the moment we will stick to f(i) = iˆ2. Again we shall start by doing an example by hand, let us determine the values of I_N for N equals one to four. Firstly N = 1 𝐼1 = 𝑖=1 𝑁 𝑖2 = 1 For N=2 𝐼2 = 𝑖=1 𝑁 𝑖2 = 1 + 4 = 5 For N=3 𝐼3 = 𝑖=1 𝑁 𝑖2 = 1 + 4 + 9 = 14 In general, 𝐼𝑁 = 𝐼 (𝑁−1) + 𝑁2
  • 4. maxN = input(’Enter the maximum value of N required: ’); I(1) = 1ˆ2; for N = 2:maxN I(N) = I(N-1) + Nˆ2; end disp([’Values of I_N’]) disp([1:N; I])
  • 5. Summing Infinite Series • The Taylor series for a function f(x) about a point x = a is given by • We can use this to approximate sin x by an infinite series in x as • Of course in using a computer we cannot actually take N to be infinity, but we shall take it to be large in the hope that the error will be small. We can write the above series (taking N = 10) and evaluate the series at the points 0, 0.1, 0.2, 0.3, 0.4 and 0.5 using
  • 6. v = 0.0:0.1:0.5; sinx = zeros(size(v)); N = 10; range = 0:N; ints = 2*range+1; for n = range sinx = sinx+(-1)ˆn*v.ˆints(n+1)/(factorial(ints(n+1))); end v = 0.0:0.1:0.5; sets up the vector v running from zero to a half in steps of a tenth. sinx = zeros(size(v)); The command size(v) gives the size of the vector v and then the command zero sets up sinx as an array of zeros of the same size. N = 10; range = 0:N; sets a variable N to be equal to 10 and then sets up a vector range which runs from zero to N. ints = 2*range+1; This gives the mathematical expression 2n+1 for all the elements of the vector range and puts them in ints. for n = range ..... end This loop structure repeats its arguments for n equal to all the elements of range. sinx = sinx+(-1)ˆn*v.ˆints(n+1)/(factorial(ints(n+1))); This mathematical expression evaluates the subject of the summation as a function of n. ints(1) refers to the value when n = 0). Also note the use of .ˆ when operating on v as this is a vector.
  • 7. IF, elseif, else statements • It is convenient at this stage to introduce some of the other commands which are available to us when constructing conditional statements, namely else and elseif. The general form of these is given by: if (expression) commands ... elseif (expression) commands ... else commands ... End • Notice that each if statement must be paired with an end statement, but we can use as many elseif statements as we like, but only one else (within a certain level). We are also able to nest if statements: again it is recommended that the new levels are indented so as to aid readability.
  • 8. Example 3.12 Consider the following piece of code which determines which numbers between 2 and 9 go into a specified integer exactly: str = ’Divisible by ’; x = input(’Number to test: ’); for j = 2:9 if rem(x,j) == 0 disp([str int2str(j)]) end end
  • 9. Example 3.13 Here we construct a conditional statement which evaluates the function: if x >= 0 & x <= 1 f = x; elseif x > 1 & x <= 2 f = 2-x; else f = 0; end
  • 10. Conditional loops • Suppose we now want to repeat a loop until a certain condition is satisfied. This is achieved by making use of the MATLAB command while, which has the syntax while (condition) commands... end • Example 3.16: Write out the values of x2 for all positive integer values x such that x3 < 2000. To do this we will use the code
  • 11. Iteration • repetition of a mathematical or computational procedure applied to the result of a previous application, typically as a means of obtaining successively closer approximations to the solution of a problem.