First Lecture
First Lecture
by
2023-2024
Contents
1 Loops 1
1.1 for-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 while-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2 Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1 Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.3 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
i
4.3 Systems of Non-linear Equations . . . . . . . . . . . . . . . . . . . . 41
ii
1 Loops
1.1 for-loop
value of a looping array. The variable that holds the current value of the looping
loops because they have a predefined begin and end. The abstract syntax of a
code block
end
A for-loop assigns the looping variable to the first element of the looping array. It
executes everything in the code block. Then it assigns the looping variable to the
next element of the looping array and executes the code block again. It continues
this process until there are no more elements in the looping array to assign.
Example
Write a Matlab script file that accepts a vector and returns the sum of all elements
1
v=input(’vector’)
s=0;
for i=1:length(v)
s=s+v(i);
end
Example
Write a Matlab script file that accepts a vector and returns the sum of all even
elements in the vector without using direct command sum (using for-loop).
v=input(’vector’)
s=0;
for i=1:length(v)
if rem(v(i),2)==0
s=s+v(i);
end
end
2
Exercise
Write a Matlab script file that accepts a vector and returns the sum of all positive
elements in the vector without using direct command sum (using for-loop).
Exercise
Write a Matlab scripts file that accepts a vector and returns the maximum element
Exercise
Write a Matlab scripts file that accepts a vector and returns the minimum element
Exercise
Write a Matlab scripts file that accepts a vector and returns the position of the
zeros (if exists) in the vector without using direct commands (using for-loop).
Exercise
Write a Matlab scripts file that accepts a vector and returns the number of zeros
3
Example
Write a Matlab script file that accepts a matrix and returns the sum of all positive
elements in the Matrix without using direct command sum (using for-loop).
M=input(’Matrix’)
a=size(M);
s=0;
for i=1:a(1)
for j=1:a(2)
if M(i,j)>0
s=s+M(i,j);
end
end
end
Exercise
Write a Matlab script file that accepts a matrix and returns the maximum element
4
Exercise
Write a Matlab script file that accepts a matrix and returns the position of the
maximum element in the Matrix without using direct command max (using for-
loop).
Example
Write a Matlab script file that accepts a matrix and returns the sum of all elements
M=input(’Matrix’)
a=size(M);
for i=1:a(1)
s=0;
for j=1:a(2)
s=s+M(i,j);
end
b(i)=s;
end
5
Exercise
Write a Matlab script file that accepts a matrix and returns the sum of all elements
Exercise
Write a Matlab script file that accepts a matrix and returns the maximum element
Exercise
Write a Matlab script file that accepts a matrix and returns the position of the
maximum element in each row without using direct command max (using for-
loop).
Exercise
Write a Matlab script file that accepts a square matrix and returns the sum of all
elements in the diagonal without using direct commands trace, sum and diag
(using for-loop).
6
Example
Write a Matlab script file that accepts a square matrix and returns the sum of
all elements in the upper triangle above the main diagonal without using direct
M=input(’Matrix’)
a=size(M);
if a(1)==a(2)
s=0;
for i=1:a(1)
for j=1:a(2)
if i < j
s=s+M(i,j);
end
end
end
else
end