Tutorial 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Tutorial: 4 SCILAB PROGRAMMING

20/02/2018

Aim:

To learn programming for simple functions in SCILAB environment with the use of input, disp, if, elseif,
for, and while loops.

Exercises:

1. The input Function

0.

syntax is :
n= input(‘promptstring’)

The function displays the promptstring , for keyboard input, and then returns the value from the

keyboard. For example,


m= input(‘Mass(kg):’)

When this line is executed, the user is prompted with the message
Mass(kg):

If the user enters a value, it would then be assigned to variable m.

2. The disp Function.

This function provides a handy way to display a value. Its syntax is:
disp(value)

Where value = the value you would like to display. It can be a numeric constant or variable, or a string
message enclosed in hyphens.

Try the following in the console window of SCILAB.

a. To a string (usually a sentence), put in between quotes.


-->disp("Bob won")
Bob won
b. To display combination of words and values use string command which converts values to
character strings using “+” between different parts:
-->d=500;

-->disp("Bob won "+string(d)+" dollars")


Bob won 500 dollars

RA1511007010075 Chemical Engineering 1


3. The if structure. This structure allows you to execute a set of statements if a logical condition is
true. Its general syntax is
If condition

Statements
end
4. The if … else Structure. This structure allows you to execute a set of statements if a logical
condition is true and to execute a second set if the condition is false . Its general syntax is

If condition

statements1

else

statement2

end
5. The if …..elseif structure. It often happens that the false option of an if ….else structure is another
decision. This type of structure often occurs when we have more than two options for a particular
problem seting. For such cases, a special form of decision structure, the if…..elseif has been
developed. It has the general syntax
If condition1

statements1

elseif condition2

statements2

elseif condition3

statements3

else

statements

else

end

RA1511007010075 Chemical Engineering 2


6. Write a program to display the results (Pass/Fail) of an exam using input, if, elseif and disp
commands
Programm for displaying of results as per average marks
Open SciNotes and type the following:
avgmark=input('enter the avg mark:');
if avgmark>=75 then
disp('passed with distinction')
elseif avgmark>=60
disp('passed with First class')
elseif avgmark>=45
disp('passed in second class')
elseif avgmark>=30
disp('failed')
end
enter the avg mark:85

passed with distinction


7. Loops
As the name, loops perform operation repetitively
There are two ways of loops i) for loops ii) while loops
For loops ends after specified no. of repetations, while loop ends on the basis of logic condition
General structure of the loop is as follows
For index= start : step : finish
Statement
end

a. Try this in the console window of SCILAB


-->for i=1:5
-->disp(i)
-->end

1.

2.

3.

4.

5.

b. Try this in the console window of SCIALB to get the values of u(n) for 20 terms
-->u(1)=4;

-->for n=1:20
-->u(n+1)=u(n)+2*n+3;
-->disp([n u(n)])
-->end

RA1511007010075 Chemical Engineering 3


1. 4.

2. 9.

3. 16.

4. 25.

5. 36.

6. 49.

7. 64.

8. 81.

9. 100.

10. 121.

11. 144.

12. 169.

13. 196.

14. 225.

15. 256.

16. 289.

17. 324.

18. 361.

19. 400.

20. 441.

c. Use of For loop to get n! values:


0!=1
1!=1
2!=1x2=2
3!=1x2x3=6
4!=1x2x3x4=24

RA1511007010075 Chemical Engineering 4


5!=1x2x3x4x5=120

function fout=factor(n)
x=1;
for i=1:n
x=i*x
end
fout=x
endfunction

-->factor(5)
ans =

120.

d. Use of while loop


While condition
Statements
end
statement between while end equated as long as condition is true.
n=8
while(n>0)
n=n-3
disp(n)
end

-->5.

2.

- 1.

e. I planted a Christmas tree in 2005 measuring 1.20 m. It grows by 30 cm per year. I decided to
cut it when it exceeds 7m. In what year I cut the tree
-->h=1.2;

-->y=2005;

-->while h<7
-->h=h+0.3;
-->y=y+1;
-->end

-->y
y =

2025.

RA1511007010075 Chemical Engineering 5


8. Write a simple programme for plotting functions as given below
For an example consider two functions f and g defined over the interval (-2≤x≤5) with 50 points
f(x)=(𝑥 2 + 2𝑥).e-x
g(x)=sin(x/2)
function y= f(x);
y= (x^2 + 2*x)* exp(-x);
endfunction
x=linspace(-2, 5, 50);
plot(x,f);
function y= g(x);
y= sin(x/2);
endfunction
x=linspace(-2, 5, 50);
plot(x,g);
clf;
plot(x,f,”r”,x,g,”g”);

9. Write a simple program to fine the simple interest. Assume principle amount to be 1000 dollars, rate
of interest to be 8% per annum and time to be 5yrs.
P= input (‘Enter the principle amount (USD): ‘);
R= input (‘Enter the rate of interest (%): ‘);
T= input (‘Enter the time period (years): ‘);
function si=f(P,R,T)
si=P*R*T/100;
disp(si)
endfunction

400

RA1511007010075 Chemical Engineering 6


10. Create a circle using built in function input in SCILAB and try the same using function command and
execute the same in command or console window
r= input('Enter radius of circle: ');
theta= linspace(0, 2*%pi, 100);
x= r*cos(theta);
y= r*sin(theta);
plot(x,y);
title('Circle of given radius,"font size", 4');

r= input('Enter radius of circle: ');


theta= linspace(0, 2*%pi, 100 );
function x=f(r, theta);
x= r*cos(theta);
endfunction
function y=f(r, theta);
y= r*sin(theta);
endfunction
plot(x,y);
title('Circle of given radius',"font size", 4);

RA1511007010075 Chemical Engineering 7


11. Determine future value of a deposit by both simple and compound interest using interactive command
input. Plot a graph with labeled legend and axis x, y between future values by simple and compound
interest vs time if the 100,000 USD is deposited with rate of interest equal to 9% p.a. Also write function
file for the given data showing the plot.

function (y=interest(t))
P=100000;
i=9;
t=input('Enter time (years): ');
n=[0:1:t]';
si= P+(P*i*n)./100;
ci= P.*((1+i/100).^n);
y= [si ci];
endfunction
plot(n,y);
xlabel('Time in years');
ylabel('Amount, USD of SI ,CI');
legend('SI','CI',1,%F);
xtitle('Comparison of SI and CI for $100000');
xgrid(1);

Enter time (years): 10

Result: Thus we learned SCILAB programming for simple functions in SCILAB environment with the
use of input, disp, if, else if, for, and while loop commands.

RA1511007010075 Chemical Engineering 8


Startup execution:
loading initial environment

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
function(p)=bisection(a,b,f,tol)
!--error 37
Incorrect function at line 1.
at line 1 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->a=-5
a =

- 5.

-->b=-3
b =

- 3.

-->deff('[y]=f(x)', ' y=2 *sin(x)-(%e^x)/4 -1')

-->tol=10^5-5
tol =

99995.

-->tol=10^5-1
tol =

RA1511007010075 Chemical Engineering 9


99999.

-->tol=10^-5
tol =

0.00001

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
if (abs f(s) < tol)then
!--error 3
Waiting for right parenthesis.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
if (abs (f(s) < tol))then
!--error 276
Missing operator, comma, or semicolon.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
if (abs (f(c) < tol))then
!--error 276
Missing operator, comma, or semicolon.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->Bisection(a,b,f,tol)
!--error 4
Undefined variable: Bisection

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)

RA1511007010075 Chemical Engineering 10


if (abs (f(c) < tol))then
!--error 276
Missing operator, comma, or semicolon.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
if (abs (f(c) < tol))then
!--error 276
Missing operator, comma, or semicolon.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
if (abs (f(c)) < tol)then
!--error 276
Missing operator, comma, or semicolon.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

-->exec('C:\Users\rishabh\Documents\tut5.sce', -1)
if (abs f(c) < tol) then
!--error 3
Waiting for right parenthesis.
at line 11 of function bisection called by :
endfunction
at line 23 of exec file called by :
exec('C:\Users\rishabh\Documents\tut5.sce', -1)

RA1511007010075 Chemical Engineering 11

You might also like