0% found this document useful (0 votes)
11 views10 pages

First Lecture

First

Uploaded by

Yousif Kawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

First Lecture

First

Uploaded by

Yousif Kawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Computer Programming

by

Dr. Alan Mohammed Omar

2023-2024
Contents

1 Loops 1

1.1 for-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 while-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2 Using the Symbolic Math Toolbox 17

2.1 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2 Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2.3 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3 Polynomials, Curve Fitting, and Interpolation 23

3.1 Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.1.1 Value of a Polynomial . . . . . . . . . . . . . . . . . . . . . . 24

3.1.2 Roots of a Polynomial . . . . . . . . . . . . . . . . . . . . . . 26

3.2 Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.2.1 Curve Fitting with Polynomials; The polyfit Function . . . . 29

3.2.2 Curve Fitting with Functions Other than Polynomials . . . . 35

3.3 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

4 Solutions to Systems of Equations 38

4.1 Systems of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . 38

4.2 A Non-linear Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 39

i
4.3 Systems of Non-linear Equations . . . . . . . . . . . . . . . . . . . . 41

ii
1 Loops

1.1 for-loop

A for-loop is a sequence of instructions that is repeated, or iterated, for every

value of a looping array. The variable that holds the current value of the looping

array is called looping variable. Sometimes for-loops are referred to as definite

loops because they have a predefined begin and end. The abstract syntax of a

for-loop block is as follows.

for looping variable=looping array

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

in the vector without using direct command sum (using for-loop).

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

of the vector without using direct command max (using for-loop).

Exercise

Write a Matlab scripts file that accepts a vector and returns the minimum element

of the vector without using direct command min (using for-loop).

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

in the vector without using direct commands (using for-loop).

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

in the Matrix without using direct command max (using for-loop).

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

in each row without using direct command sum (using for-loop).

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

in each column without using direct command sum (using for-loop).

Exercise

Write a Matlab script file that accepts a matrix and returns the maximum element

in each row without using direct command max (using for-loop).

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

commands sum and diag (using for-loop).

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

’The matrix is not square’

end

You might also like