0% found this document useful (0 votes)
7 views

Assessment 3

Uploaded by

asdf
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)
7 views

Assessment 3

Uploaded by

asdf
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/ 5

Assessment 3

Table of Contents
Question one part a .............................................................................................................................. 1
Question one part b ............................................................................................................................. 1
Question one part c .............................................................................................................................. 1
Question two part a ............................................................................................................................. 1
Question two part b,c & d ..................................................................................................................... 2
Question three .................................................................................................................................... 4

by Diego Colmenero 24426652

Question one part a


The mod command gives you the remainder of the division of two numbers. For example, if you write mod (7,3)
Matlab is going to divide 7/3 which is equal to 2 with a remainder of 1 and the output will be that 1. Same thing
happens if you write mod (14,5), Matlab will make the division and give you the remainder (4).

Question one part b


a=21;
if mod (a,2)==1
disp (a + " is an odd number")
else
disp (a + " is an even number")
end

21 is an odd number

Question one part c


x=length ('Diego');
if mod (x,2)==1
disp ("lenght of Diego is an odd number")
else
disp ("lenght of Diego is an even number")
end

lenght of Diego is an odd number

Question two part a


a=(1:10)
b=1./2.^a
c=sum (b)

a =

1
Assessment 3

1 2 3 4 5 6 7 8 9 10

b =

Columns 1 through 3

0.500000000000000 0.250000000000000 0.125000000000000

Columns 4 through 6

0.062500000000000 0.031250000000000 0.015625000000000

Columns 7 through 9

0.007812500000000 0.003906250000000 0.001953125000000

Column 10

0.000976562500000

c =

0.999023437500000

Question two part b,c & d


format long %shows more decimal numbers.
for n=1:20 %creates a loop that iterates 20 times.
a=(1:n); %creates an object that contains the numbers from 1 to 20.
b=1./2.^a; %creates another object that contains all the results from
applying "a" to an expression.
S_n=sum (b); %creates an object "S_n" that makes a summation with all
the values of "b" for each loop.
S(n)=S_n; %alocates each value of "S_n" into an array "S" for each loop.
end %finishes the loop.

S %displays the array "S".


S_n %displays the number "S_n".
n=1:20 %creates a list of values that contains every integer from 1 to 20.
plot (n,S,'bo-') %makes a graph with all the values of n in the x axis and
all the values of S in the y axis.
hold on %retains current plot when adding new plots.

% Matlab gives an error when you use an array of 10 ones because the
% program needs all the arrays of the plot to have the same dimension so I
changed the array
% to have the same number of ones as elements has "n" (I did this to make
the program as universal as possible).
y=ones (1,length(n)); %creates a lists of 20 ones.

2
Assessment 3

plot(n, y, 'r--'); %plots a dashed horizontal line in the previous graph.

xlabel('n value') %lables the x axis


ylabel("S_n=1/2^a") %lables the y axis
title ("24426652") %establishes a title
hold off %turns the hold command off

% From the plot we can deduce that the bigger the value of "a" is, the more
% the function approaches 1. So the value of the limit when n tends to
infinity would be
% 1.

S =

Columns 1 through 3

0.500000000000000 0.750000000000000 0.875000000000000

Columns 4 through 6

0.937500000000000 0.968750000000000 0.984375000000000

Columns 7 through 9

0.992187500000000 0.996093750000000 0.998046875000000

Columns 10 through 12

0.999023437500000 0.999511718750000 0.999755859375000

Columns 13 through 15

0.999877929687500 0.999938964843750 0.999969482421875

Columns 16 through 18

0.999984741210938 0.999992370605469 0.999996185302734

Columns 19 through 20

0.999998092651367 0.999999046325684

S_n =

0.999999046325684

n =

Columns 1 through 13

3
Assessment 3

1 2 3 4 5 6 7 8 9 10 11 12
13

Columns 14 through 20

14 15 16 17 18 19 20

Question three
x=[1 9 7 6 3 0 8 7 5 2 1 6]
y=3

for i= (1:(length (x)))


if mod (x(i), y)==0
disp ("the term " + x(i) + " is divisible by " + y)
else
disp ("the term " + x(i) + " is not divisible by " + y)
end
end

x =

1 9 7 6 3 0 8 7 5 2 1 6

4
Assessment 3

y =

the term 1 is not divisible by 3


the term 9 is divisible by 3
the term 7 is not divisible by 3
the term 6 is divisible by 3
the term 3 is divisible by 3
the term 0 is divisible by 3
the term 8 is not divisible by 3
the term 7 is not divisible by 3
the term 5 is not divisible by 3
the term 2 is not divisible by 3
the term 1 is not divisible by 3
the term 6 is divisible by 3

Published with MATLAB® R2024a

You might also like