0% found this document useful (0 votes)
14 views5 pages

Matlab 3

Uploaded by

RISHABH
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)
14 views5 pages

Matlab 3

Uploaded by

RISHABH
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

Assignment No.

03
Q-1.Create a new m file and save as m file and execute following and discuss the value
of y.

a=10;
while a>0
y(a)=a*10;
a=a-1;
end
y

Result:y= 10 20 30 40 50 60 70 80 90 100
Explanation: a,y is a variable, in which 10 is assigning in a. By applying while loop this code is iterate ten
time sand in every iteration a unique value is store in y. First of all compiler is taken a value of 10 and
compare with condition (a>0) if condition is satisfies then that operation value is store in ‘y’. After that, the
value of a is decrement by 1.This loop is executed again and again until condition is satisfied. Once ‘a’
become 1, new value is store in ‘a’ is 0.Compiler is compare this value with condition and come out from
loop.

Q-2.Examine the following for loop sand determine how many times each loop will be executed:
i) for index=7:10
ii) for jj= 7:-1:10
iii) for ii= -10:3:-7
iv) for kk = -10:3:10
v) for ii= 0:0.25:5
vi) for jj= 5:-0.5:-5

1)For index= 7:10

1
RISHABH SINGH
24ME51D
Syntax:forVariableName=a:b

Result: 78 9 10
Explanation: This loop runs from 7 to 10. Therefore, it will execute 4 times (for index values7, 8, 9,
and 10).
ii) for jj= 7:-1:10
Syntax:forVariableName=a:b
Result: error
Explanation: The loop variable JJ start at 7 and is supposed to decrement by 1 each iteration
until it reach 10. However, this is an in correct setup because the loop is trying to in crement
from 7 to 10 using a decrement step size.
iii) forii=-10:3:-7

Syntax: for VariableName=a:b

Result: -10-7
Explanation:The loop variable ii start at -10 and is supposed to Increment by 3 each iteration
until it reaches -7.
iv) for kk =-10:3:10
Syntax: for Variable Name =a:b
Result: -10 -7 -4 -1 2 5 8
Explanation: The loop variable kk start at -10 and is supposed to increment 3 each iteration.
Until it reaches 10.
v) forii= 0:0.25:5
Syntax:forVariableName=a:b
Result: 0 0.2500 0.5000 0.7500 1.0000 1.2500 1.5000 1.7500 2.0000 2.2500
2.5000 2.7500 3.0000 3.2500 3.5000 3.7500 4.0000 4.2500 4.5000 4.7500
5.0000

Explanation: The loop variable ii start at 0, and is supposed to increment 0.25 each iteration until it
reaches 5.
vi) forjj= 5:-0.5:-5
Syntax:forVariableName=a:b

2
RISHABH SINGH
24ME51D
Result:5.0000 4.5000 4.0000 3.5000 3.0000 2.5000 2.0000 1.5000 1.0000 0.5000
0 -0.5000 -1.0000 -1.5000 -2.0000 -2.5000 -3.0000 -3.5000 -4.0000 -4.5000
-5.0000
Explanation: The loop variable jj start at 5 and is supposed to decrement 0.5 each
iteration until it reaches -5.
Q-3.Write a loop to construct the following vectors:
(a) v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
(b) v = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
(c) v = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
(d) v = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

i) Syntax:>>v =[ ];
>>fori=1:10;
v(i) = i;
end
Result: v = [1,2, 3, 4, 5,6, 7, 8, 9, 10]
Explanation: First of all create a vector v then iterate the loop 10 times and store the value of i in v.
ii) Syntax:>>v=[ ];
>> for i = 0:10;
v(i+1)=i*0.1;
end
Result: v = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
Explanation: First of all create a vector v then iterate the loop 11times.After that using
arithematic operation, multiple i with 0.1 and add 1 in every iteration then store in v.
iii) Syntax:>>v = [];
>>fori=1:10; v(i)
= 2*i;

end
Result: v =[2, 4,6, 8, 10,12, 14,16, 18, 20]
Explanation: First of all create a vector v then iterate the loop 10 times. After that using
arithematic operation, multiple i with 2 and store in v.

3
RISHABH SINGH
24ME51D
iv) Syntax:>>v=[];

>>fori=1:10; v
= [v,1];
end
Result:v = [1,1, 1, 1, 1,1, 1, 1, 1, 1]
Explanation: First of all create a vector v, then using ‘for’ loop iterate the loop 10 times after that,
then ext command is [v,1] it mean that add 1 to the end of vector v but the vector v is empty. So in
every iteration 1 is execute.

Q-4.A while Loop executes a set of commands repeatedly, until a controlling expression is no longer
true. Try running this simple program from an m-File:
clc; clear all;
b=0; t=0; %Enter the initial values of variables b and t while
2^b<200 %Enter the controlling expression
B=b+1;
T=t+2^b; %These two lines are the commands to be executed
End %End the While Loop
Result:b=8,t=510
Explanation: first of all initialize the value of ‘b’ and ‘t’=0.Then using the while loop with a
condition ( 2^b<200) this loop is iterated upto b=7 after that the loop is terminated. The new
value of ‘b’and ‘t’is 8 and 510 respectively.

Q-5.Type the following lines in matlab editor and save it as Q5test.mandexecuteit.


clc; clear all;
a=input('enter value of a=');
sum= 0.0;
if a < 50

4
RISHABH SINGH
24ME51D
sum = sum +a;

end

disp('sum');
disp(sum);
Result: enter the value of a=
Sum 49
Explanation: First of all, giving a command of a=input('enter value of a = ') it will display a
(enter a value of a =) in command window. Now initialize a value of sum = 0.0. After that
compare it by condition (a<50) if it is true then it will execute next command which is sum =
sum+a; and store it in sum. disp(sum) show the value of sum and disp(‘sum’) show the sum in
command window. If condition is false then it will execute sum=0. For example take a=49 as a
input value and in output sum=49 is executed.

5
RISHABH SINGH
24ME51D

You might also like