Anda di halaman 1dari 23

Exercise 1.

1
Exercise 1: Scilab Environment
1. What are the other ways of obtaining online help in Scilab, other than typing help
at the command prompt?
Ans: From menu? Scilab Help or press F1 key.
2. Where are the demo programs that demonstrate Scilabs capabilities?
Ans: Go to the menu? Scilab Demos
3. How can you search for a word or pattern in the Scilab online help browser?
Ans:
4. Can you change the font used by Scilab?
Ans: Go to the menu Preferences Choose Font
5. What is the command to clear the screen?
Ans: clc
6. What is the short cut key to clear the screen?
Ans: F2 key
7. What is command history? What are the shortcut keys to use the command history?
Ans: Go to the menu Edit History
8. Can you use the built-in variable ans in your calculations? If so, is it a good idea to do so or is it
better to use your own named variables?
Ans: Usually the answer of a calculation is stored in a variable so that it could be used in the
immediately subsequent expressions. If the user does not explicitly supply the name of the variable to
store the answer returned by an expression, Scilab uses a variable named ans to store such results. If
the user specifies a variable to store the results of an expression, results are stored in the user specified
variable.
9. When do you think it is useful to use the semicolon (;) to suppress the output of a Scilab statement?
Ans: Ending an expression with a semicolon (;) suppresses the echo of the output of an expression.
The expression is evaluated and results assigned, but the results are not echoed.
10. What are the rules for choosing names for variables in Scilab? Can you use a numeric character as
the first character? Can you use underscore (_) as the firstcharacter? Can you use special characters,
such as ,+, /, ? in a variable name?
Ans:
11. List areas within your proposed branch of specialization where Scilab can be a useful tool.
Ans:
12. What is the help diary?

Ans: A command to record all commands that you type and save them to a file so that you can see
them later
13. Where can you find the Scilab current working directory?
Ans: From the menu File Get Current Directory
14. What is the command to find the Scilab current working directory?
Ans: pwd or getcwd
15. How can you change the Scilab current working directory to a different location?
Ans: cd (directory) or go to the menu File Change Directory and choose the directory you want
to go to in the dialog box
16. Why is it important to know the Scilab current working directory?
Ans: Because it is the default location where Scilab saves all files, unless you explicitly specify the
full path.
17. What are the available data types in Scilab?
Ans: Type the command help type

Exercise 2: Arithmetic Operations


1. What are the results of the following computations (think about it before trying in
Scilab)?

-->2*3+4
ans =

10.

-->2*3-4
ans =

2.

-->2+3*4
ans =

14.

-->2/3+4
ans =

4.6666667

-->2+3/4
ans =

2.75

2. What are the results of the following computations (think about it before trying in
Scilab)?

-->2*(3+4)
ans =

14.

-->(2+3)*4
ans =

20.

-->(2+3)/4
ans =

1.25

-->3/(2+4)
ans =

0.5

3. What are the results of the following computations (think about it before trying in
Scilab)?

-->1.23456789d10
ans =

1.235D+10

-->1.23456789e10
ans =

1.235D+10

-->1.23456789e-5
ans =

0.0000123

4. What are the results of the following computations (think about it before trying in
Scilab)?

-->sqrt(4)
ans =

2.

-->sqrt(9)
ans =

3.

-->sqrt(-1)
ans =

-->sqrt(-2)
ans =

1.4142136i

-->exp(1)
ans =

2.7182818

-->log(exp(2))
ans =

2.

-->exp(log(2))
ans =

2.

-->10^2
ans =

100.

-->log(10^2)
ans =

4.6051702

-->10^log10(2)
ans =

2.

5. What are the results of the following computations (think about it before trying in
Scilab)?
-->cos(0)
ans =

1.

-->sin(0)
ans =

0.

-->cos(%pi)
ans =

- 1.

-->sin(%pi)
ans =

1.225D-16

-->cos(%pi/4)-sin(%pi/4)
ans =

1.110D-16

6. Determine the following calculations using SCILAB if


a = 2.3, b = 2.3, c = /2, x = 2/ and y = 3.
Write down your answer.

-->2.3^2+(-2.3)*%pi/2+2/%pi
ans =

2.3137882

-->sin(%pi/2)+sqrt(3)/%pi/2

ans =

1.2756644

-->2.3+%pi/2/2/%pi+sqrt(3)
ans =

4.2820508

-->1/cos(%pi/2)+log(2/%pi)
ans =

1.633D+16

-->(2.3+%pi/2)^3/(-2.3)
ans =

- 25.215822

7. Using the same values of a, b, c, x and y given above, check whether the following
Boolean statements are true or false. Write down your answer.
(a) a > c
-->a>c
ans =

F
(b) a = b
-->a=b
a =
- 2.3

(c) (2a + b)/x2 < 1


-->2*2.3+(-2.3)/(2/%pi)^2<1
ans =

(d) x + 2ab + b2 23
-->x+2*a*b+b^2
ans =

- 4.6533802

-->ans<=23
ans =

(e) 2ac = 2cb


-->2*a*c=2*c*b
ans =
F

Exercise 3 : Working on Arrays


1. Can you use the .+ operator like you can use .*?
Ans: No.
2. What is the size of an empty matrix a = [ ]?
Ans: Size 0*0
3. While generating a range, can you specify a negative increment?
Ans: Yes, if the start vale is greater than the end value.
4. What is the command to generate values from 0 to 2 an increment of /16?
Ans: 0:%pi/16:2*%pi
5. What is the command to extract the diagonal elements of a square matrix into a vector?
Ans: a=diag(x) creates a vector a containing the diagonal elements of matrix x
6. Given a square matrix A, how can you create a matrix B whose diagonal elements are the same as
those of A but the other elements are all zero?
Ans: b=eye (a).*a
7. Create a matrix of size 5 5 having the required elements on the diagonal, above the diagonal and
below the diagonal.

Ans: -->a=diag([1:5])
a =

1.

0.

0.

0.

0.

0.

2.

0.

0.

0.

0.

0.

3.

0.

0.

0.

0.

0.

4.

0.

0.

0.

0.

0.

5.

8. Create a matrix of size 5 5 having the required elements on the diagonal above the main diagonal.
Ans: -->b=diag([1:4],1)
b =

0.

1.

0.

0.

0.

0.

0.

2.

0.

0.

0.

0.

0.

3.

0.

0.

0.

0.

0.

4.

0.

0.

0.

0.

0.

9. Create a matrix of size 5 5 having the required elements on the diagonal below the main diagonal.
Ans: -->b=diag([1:4],-1)
b =

0.

0.

0.

0.

0.

1.

0.

0.

0.

0.

0.

2.

0.

0.

0.

0.

0.

3.

0.

0.

0.

0.

0.

4.

0.

10. Create a tridiagonal matrix of size 5 5 with the specified elements on the main diagonal above
and below the main diagonal.
Ans: -->a=diag([1:5])+diag([6:9],1)+diag([10:13],-1)
a =

1.

6.

0.

0.

0.

10.

2.

7.

0.

0.

0.

11.

3.

8.

0.

0.

0.

12.

4.

9.

0.

0.

0.

13.

5.

11. If a = [1 2 3; 4 5 6; 7 8 9]; Find the following operations on that matrix.


-->a=[1 2 3; 4 5 6; 7 8 9]
Ans: a =

1.

2.

3.

4.

5.

6.

7.

8.

9.

-->b=a'

b =

1.

4.

7.

2.

5.

8.

3.

6.

9.

-->c=a+b
c =

2.

6.

10.

6.

10.

14.

10.

14.

18.

-->d=a-b
d =

0. - 2. - 4.
2.

0. - 2.

4.

2.

0.

-->e=a*b
e =

14.

32.

50.

32.

77.

122.

50.

122.

194.

-->f=[3 1 2; 1 5 3 ; 2 3 6]
f =

3.

1.

2.

1.

5.

3.

2.

3.

6.

-->g=inv(f)
g =

0.4285714 0.
0.

- 0.1428571

0.2857143 - 0.1428571

- 0.1428571 - 0.1428571 0.2857143

-->f*g
ans =

1.

0.

0.

0.

1.

1.110D-16

0.

0.

1.

-->det(f)
ans =

49.

-->log(a)
ans =

0.

0.6931472 1.0986123

1.3862944 1.6094379 1.7917595


1.9459101 2.0794415 2.1972246

-->a.*b
ans =

1.

8.

21.

8.

25.

48.

21.

48.

81.

-->a^2
ans =

30.

36.

42.

66.

81.

96.

102.

126.

150.

-->a.^2
ans =

1.

4.

9.

16.

25.

36.

49.

64.

81.

12. Commonly used matrices, such as zero matrices, identity matrices, diagonal matrices,
matrix containing randomly generated numbers are created using the following
commands:
a=zeros(5,8)
-->a=zeros(5,8)
a =

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

0.

b=ones(4,6)

-->b=ones(4,6)
b =

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

c=eye(3,3)
-->b=ones(4,6)
b =

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

1.

d=eye(3,3)*10
-->d=eye(3,3)*10
d =

10.

0.

0.

0.

10.

0.

0.

0.

10.

Exercise 4 : Looping and Branching


1. Use the if-elseif-else statement to allocate a grade given a percentage mark according to the
following schedule.
MARK
GRADE

90+
A+

85+
A

80+
A-

Ans:
//Exercise 4
//Question 1
marks=input ('Enter your marks:')
if marks>=90 then
disp ('A+')
else if (marks>=85)&(marks<90)
disp ('A')
else if (marks>=80)&(marks<85)
disp ('A-')
else if (marks>=65)&(marks<80)
disp ('B')
else
disp ('C')
end
end
end
end

-->exec('C:\Users\user\Documents\sem 5\smq\ex4.sce', -1)


Enter your marks:44
C

2. Create a for loops that display numbers from 5 to 1.

// Exercise 4
// Question 2
disp('Q2')
for i=5:-1:1
disp(i)
end

65+
B

50+
C

-->exec('C:\Users\acer\Desktop\Ex4Q2.sce', -1)
Q2
5.
4.
3.
2.
1.

3. Create a for loops that display numbers 1, 3, 5.

//Exercise 4
// Question 3
for i=1:2:5
disp(i)
end
-->exec('C:\Users\acer\Desktop\Ex4Q3.sce', -1)

1.

3.

5.

4. Create a for loops that display numbers 1,1,3.

//Exercise 4
//Question 4
disp ('Q2')
for i =1:-2:-3
disp(i)
end
-->exec('C:\Users\acer\Desktop\E4Q4.sce', -1)

Q2

1.

- 1.

- 3.

5. Create a for loops that display numbers 1.3, 3.5, 5.7.

//Exercise 4
//Question 5
disp('Q5')
for i=1.3:2.2:5.7
disp(i)
end

-->exec('C:\Users\acer\Desktop\E4Q5.sce', -1)

Q5

1.3

3.5

5.7

6. Write three distinct for-loops that give the numbers 5, 4, 3, 2, 1 as output in decreasing order; one
with the index i starting with 5 and ending with 1; another with the index j starting with 1 and ending
with 5; and a third with the index k starting with -2, with increment 2 and ending with 6.

//Question 6
disp('i')
for i =5:-1:1
disp(i)
end

-->exec('C:\Users\acer\Desktop\E4Q6.sce', -1)

5.

4.

3.

2.

1.

//Question 6
disp('j')
for i =1:1:5
disp(i)
end
-->exec('C:\Users\acer\Desktop\E4Q6.sce', -1)
j
1.
2.
3.
4.
5.
//Question 6
disp('k')
for i =-2:2:6
disp(i)
end
-->exec('C:\Users\acer\Desktop\E4Q6.sce', -1)
k
- 2.

0.
2.
4.
6.

7. Write a for-loop to determine whether the value of the index outside the loop is the same as the last
acceptable value of the index inside the loop.
8. Write a program segment to determine how many times the statements within a for-loop are
executed. How can this be calculated from the values of a, h, b?
9. Write a script file to calculate the squares of the integers up to 20.

//Question 9
for i=2:4
y=(i^2)
disp(y)
end
-->exec('C:\Users\acer\Desktop\Ex4Q9.sce', -1)

4.

9.

16.

10. Write a script file calculate the squares of the odd numbers up to 21.

11. Write a script file that display the squares of the odd numbers up to 21 backwards.
12. In the sequence 3, 7, 15, 31, . . . the first number is 3 and all the others are found by multiplying
the previous number by 2 and then adding 1. Write a script file called sequence.m which produces the
first 10 numbers in this sequence.
13. Write down the output for the following code.
for k = [1:3]
mprintf(%.1g times through the loop \n , k)
end

14. Now modify the code given in Question 1 such that it will display the following output:
1.0 times through the loop.
2.0 times through the loop.
3.0 times through the loop.
15. Using the similar concept in Question 1 and Question 2, write a program that asks the user to input
his or her name, then displays the name three times. You must use a for loop to produce the output.
For example, if Aliza is the input name, it should display:
Hello 1, Aliza.
Hello 2, Aliza.
Hello 3, Aliza.
16. Write a Scilab script that asks the users for three values as a vector (i.e., in a form like [1 47 12])
and displays the largest value.
17. Now using a while loop, modify the code given in Question 4 such that ask the user to input a
vector of numbers, with any number of elements, and tells the user what the largest element is. Heres
what a sample run of your program might look like:
Enter a vector of numbers: [1 942 11.6 2000 12]
The largest number in that vector was 2000.
18. A year is a leap year if it is divisible by 4, unless it is a century year that is not divisible by 400.
Write a Scilab program that accepts a year and returns whether it is a leap year or not. The standard
algorithm on computing the leap year is given below:
if year modulo 400 is 0 then
is_leap_year
else if year modulo 100 is 0 then
not_leap_year
else if year modulo 4 is 0 then
is_leap_year
else
not_leap_year
19. Write a program that computes the addition of two matrices using a nested for loops. The program
requires two inputs A and B that are matrices and display the output as matrix C. The program must
able to compute the addition of any matrices given as a input of any size.

Exercise 5: Functions
1. Write a Scilab function file for the given functions below. The function should take
input x and display the output as y.
(a)

f 1 ( x )= ||
||

x if x< 0
x
if 0 x <1
f 2( x )
(b)
x +1if 1 x<2
0 elsewhere

(c)

8.17
f 3=

|x|

( 3.23 + x )
2

1.23 . sin ( x )+3.21 . cos ( 2 x )

2. Write a Scilab function name myloop.sci that calculate the following summation. You are required
to use a for loop.
n

S n=
k=1

1
n

3. Now make modification to Question 2. using a while loop. The function should be known as
myloop2.sci.
4. Write a function file that enter a input number M and then displays the integers and their squares
while the square is less than M.
5. Write a Scilab function to calculate the length of a line in the xy plane, given the coordinates of its
two ends (x1, y1) and (x2, y2). The length of such a line is given by the expression

l=( x 2x 1 )2 +( y 2 y 1 )2 Save the function as length.sci.


6. Write a Scilab function to evaluate
w(x) = 3 cos(x) +4 sin(x)
7. Write a Scilab function to evaluate the function

f ( x )=

2 x +1 x
e
x +3

8. Write a Scilab function to find the volume and surface area of a cylinder with given height and
diameter. The information used is the diameter and the height, so these will be the input parameters.
The values to be calculated are the volume and surface area; these will be the output parameters.
9. If $P is invested at an annual interest rate of i for n years and the interest is compounded annually,
the amount A of money after n years is
A = P(1 + i/100)n

Write a Scilab function that takes P, i and n as input and returns the output parameter A. Use the
function to calculate the amount for P = 1000, i = 5 and n = 2. This function is saved as a separate file
called interest.sci on your computer.
10. Write a Scilab function known as population.sci to calculate a vector for N = 30 periods of a
population modelled by the difference equation
Pn = Pn1 + kPn1(A Pn1)
for a given P0 = 100.
11. Imagine a hot cup of coffee on an outdoor table on a cold winter day. The coffee will get colder
with time. The rate at which the coffee cools is proportional, approximately, to the difference between
the temperature of the coffee and that of the surrounding air. Let Tn be the temperature of the coffee
after n minutes and let Ta be the (constant) air temperature. The difference equation
Tn = Tn1 k(Tn1 Ta)
where k > 0, approximates Newtons Law of Cooling. If T0 = 70C, k = 0.01 and Ta = 20C, write a
Scilab function to estimate the temperature of the coffee after 30 minutes.
12. The Frobenius norm of a matrix A = [aij ] with n rows and m columns is defined as the squareroot
of the sum of the squares of each of the elements of the matrix, i.e.,

AF =

i=1 j=1

a ij

Define a function AbsM(A) that calculates the Frobenius norm of a matrix.


13. Write a function file that produce the following 2 2 matrix

x 1+ x 2 (x 1+ x 2 )2
J (x )=
, with x= x 1
x 1 x2 x 1+ x2
x2

[]

Find the value for that matrix when x = [2;1].

Anda mungkin juga menyukai