Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2K views

Exercises On Matlab Functions

The document describes 5 MATLAB functions: 1. A function to calculate a repeating product to n terms using a for loop. 2. A function to determine the number of terms needed for a product to exceed a given number N using a while loop. 3. A function to convert elements of a row matrix to 0 or 5 depending on if they are negative or positive using for and if/else statements. 4. A function to convert elements of a 2D matrix to 0 or 5 depending on if they are negative or positive using nested for loops. 5. A function to evaluate a continued fraction represented by a row vector using a for loop.

Uploaded by

UAE_bboy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Exercises On Matlab Functions

The document describes 5 MATLAB functions: 1. A function to calculate a repeating product to n terms using a for loop. 2. A function to determine the number of terms needed for a product to exceed a given number N using a while loop. 3. A function to convert elements of a row matrix to 0 or 5 depending on if they are negative or positive using for and if/else statements. 4. A function to convert elements of a 2D matrix to 0 or 5 depending on if they are negative or positive using nested for loops. 5. A function to evaluate a continued fraction represented by a row vector using a for loop.

Uploaded by

UAE_bboy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercises on MATLAB functions (2)

(1) Write a function that would evaluate the following function to n terms:

P = 1 × 1.2 × 1.4 × 1.6 × ....... × [1 + 0.2(n − 1)]

Use the for loop.

function[y]=repeatprod(n)
q=1;
for m=1:n
p=1+.2*(m-1);
q=q*p;
end
y=q;

(2) Write a function that would determine the number of terms needed so that the
product P = 1 × 3 × 5 × 7 × 9 × ......(2n − 1) just exceed a given number, N. Use the
while loop.

function[y]=prodlimit(N)
p=1;m=0;
while p<=N
m=m+1;
p=p*(2*m-1);
end
y=m;

(3) Write a function that would take as input a row matrix of any length and converts
each of its elements to either 0 or 5 according to the following rule: If the element
is a negative number, convert to 0, if it is zero or positive, convert to 5. Use the
for and the if …. else statements.

function[y]=sortrow(x) %Here both x and y will be row matrices


L=length(x); %L is the number of elements in x
K=zeros(1,L); %initialise a zero row matrix of length L
for m=1:L
A=x(1,m); %take each element in x one at a time
if A<0
K(1,m)=0;
else
K(1,m)=5;
end
end
y=K;

1
(4) Write a function that would take as input an m × n matrix of any size and converts
each of its elements to either 0 or 5 according to the following rule: If the element
is a negative number, convert to 0, if it is zero or positive, convert to 5. Use the
for and the if …. else statements. (Note Example 4.4 on page 12 of your notes .
You need two for loops to address each element of an m × n matrix).
function[y]=sortmatrix(x) %x and y are matrices
L1=length(x(:,1)); %x has L1 rows
L2=length(x(1,:)); %x has L2 columns
%x is a L1 by L2 matrix.
K=zeros(L1,L2) %set aside a zero matrix of size L1 by L2
for m=1:L1
for mm=1:L2
A=x(m,mm); %take each element of x one at a time
if A<0
K(m,mm)=0;
else
K(m,mm)=5;
end
end
end
y=K;

1
(5) The expression, CF = , where the a’s are integers is called a
1
a1 +
1
a2 +
1
a3 +
a4
continued fraction. One shorthand for this is the row vector: [a1 a 2 a 3 a 4 ].
1
For example, [2 6 5] = = 0.4627 . Write a function that takes as an
1
2+
1
6+
5
input a row vector of any length and evaluates the continued fraction that it
represents. Use the for loop.

function[y]=confunc(x) %x is a row vector and y a number


x=fliplr(x); %fliplr = "flip left right"
%Vector x is flipped so that last element is first, first is last.
%because we start from the last element of x and work to the
first.
L=length(x);
a=x(1,1);
for m=2:L;
a=x(1,m)+1/a;
end;
y=1/a;

You might also like