Tutorial 4
Tutorial 4
Tutorial 4
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:
0.
syntax is :
n= input(‘promptstring’)
The function displays the promptstring , for keyboard input, and then returns the value from the
When this line is executed, the user is prompted with the message
Mass(kg):
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.
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
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
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.
function fout=factor(n)
x=1;
for i=1:n
x=i*x
end
fout=x
endfunction
-->factor(5)
ans =
120.
-->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.
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
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);
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.
-->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.
-->tol=10^5-5
tol =
99995.
-->tol=10^5-1
tol =
-->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)
-->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)