Scilab

Scilab
●
Solve a system of equations
●
Find the roots of a polynomial Equation
●
Defining Functions and plot
●
if, select, for, while statements in Scilab
●
break and continue statements
●
Structures in Scilab
Scilab
Solve the following system of Equations
3 x + 2 y = 10
1.5 x – 2 y = 10
Scilab
Solution by using Scilab
First convert the system of equations into a
matrix form:
[
]{ } { }
3
2 x 10
=
1.5 −2 y 10
Scilab
Or,
A*b=c
Where
A=
[
3
2
1.5 −2
{}
b= x
y
{ }
c= 10
10
]
Scilab
--> a=[3 2 ; 1.5 -2],
a =
c=[10;10]
3. 2.
1.5 - 2.
c =
10.
10.
--> b = inv(a) * c
b =
4.4444444
- 1.6666667
// Check the result
// A*b ?= c ?
--> a*b
ans =
10.
10.
Scilab
Solve the following system of Equations
3 x + 2 y – 4 z = 10
1.5 x – 2 y + 10 z = 10
x +
y +
z = 10
Scilab
Finding Roots of a Polynomial
-->x=poly(0,"x")
x =
x
-->p = 3*x^3 + 2*x^2 - 10*x + 3
p =
2
3
3 - 10x + 2x + 3x
-->roots(p)
ans =
- 2.3027756
1.3027756
0.3333333
Scilab
Plot the polynomial onto a figure
// First, create a function that outputs the value of the polynomial
function y = poly_cubic(x)
y = 3 - 10*x + 2*x^2 + 3*x^3;
endfunction
// now create the x variable
x= -3 : 0.01 : 3;
lx = length(x);
yp = zeros(lx,1);
for i=1:lx
yp(i) = poly_cubic( x(i) );
end
plot(x, yp)
xgrid(3)
// makes a grid in the plot windows
xtitle("POLYNOMİAL","x value", "yp value")
Scilab
Scilab: The if statement
if ( %f ) then
disp ( " Hello ! " )
else
disp ( " Goodbye ! " )
end
Scilab: The select statement
i = 2
select i
case 1
disp
case 2
disp
case 3
disp
case
disp
end
( " One " )
( " Two " )
( " Three " )
( " Other " )
Scilab: The for statement
for i = 1 : 2 : 5
disp ( i )
end
v = [1.5 exp(1) %pi ];
for x = v
disp ( x )
end
1.
3.
5.
1.5
2.7182818
3.1415927
Scilab: The while statement
s = 0
i = 1
while ( i <= 10 )
s = s + i
i = i + 1
end
Equivalent to
sum( 1:10 )
Scilab: The break and continue
statements
s = 0
i = 1
while ( %t )
if ( i > 10 ) then
break
end
s = s + i
i = i + 1
end
Scilab: The break and continue
statements
In the following example, we compute the sum s = 1 + 3 + 5 + 7 + 9 = 25. The
modulo(i,2) function returns 0 if the number i is even. In this situation, the script
goes on to the next loop.
s = 0
i = 0
while ( i < 10 )
i = i + 1
if ( modulo( i , 2 ) == 0 ) then
continue
end
s = s + i
end
Equivalent to
sum( 1:2:9 )
Scilab: Structures
Kisisel_Bilgi.isim = “Temel Aktürk”;
Kisisel_Bilgi.tckimlik = 188803456432;
Kisisel_Bilgi.dt_yil = 1988;
Kisisel_Bilgi.dt_ay = 5;
Kisisel_Bilgi.dt_gun = 24;
Kisisel_Bilgi =
tckimlik: 1.888D+11
dt_yil: 1988
dt_ay: 5
dt_gun: 24
Scilab