# Scilab

## Chapter 6 Functions

Chapter 6.1 Introduction
In every programming language, e.g. C++, Pascal and even Assembler, there are objects such as functions. This also applies to Scilab and they make the life of programmers much easier. Unlike other languages, Scilab (and Matlab) is unrivaled as a tool for performing calculations. Even solving differential equations, which we’ll cover later.
Once you learn how to make pork chops, you won’t have any problems with the next ones. It’s similar with programming. Once you solve a problem in the form of a few lines of a program, you will save them as a function. Then you no longer have to laboriously enter many lines of the program, just one function. It looks like a regular instruction. How much work will we save! And we’re not worried that something in this part of the program won’t work. After all, you’ve checked this many times before.
Chapter 6.2 The simplest function with one input and output
For example, we want to calculate the square of some number, e.g. x1=2. The input here is, for example, x1=2 and the output y=x²=2²=4
P6-1 program

```clear;clc;
function y=square(x)
y=x^2
endfunction
x1=4
z=square(x1)
disp("The square of ",x1,"is ",z)
x2=5
z=square(x2)
disp("The square of ",x2,"is ",z)
```

Program description
Function
z=square(x1)
we treat it similarly to the instruction in which
– z is a output data
– square function name
– (x1) input data
When a program encounters a function during execution
z=square(x1)
this:
– will jump to the definition of this function, i.e. function y=square(x)
– will execute what is defined in the function between the lines function y=square(x) and endfunction
that is, it will substitute the value of x1 =4 for x
x=x1²=2²=4
It will then execute the display instruction
As a result of the function, the following text was created

“The square of ”
4.
“is “
16.
“The square of ”
5.
“is “
25
Here the function consists of only one instruction y=x² and using the function does not provide any benefits. But when a function has thousands of lines with different inputs, graphs or even with such fountains as media mp4?
Chapter 6.3 Simple function with 2 inputs and 2 outputs
The function [x,y]=freak(a, b) is so simple that you don’t know what it’s for.
And this is to understand the very idea of ​​a function with multiple inputs and outputs.
P6-2 program

```clear,clc
function [x,y]=freak(a,b)
x=a+b
y=a-b
endfunction
[q,z]=freak(4,7)
disp(q)
disp(z)```

Program description
Function
[x,y]=freak(a,b)
we treat it similarly to the instruction in which
-[x,y] 2 outputs
– freak function name
– (a,b) 2 inputs
When the program encounters a function
[q,z]=freak(4,7) is:
– will jump to the definition of this function, i.e. to [q,z]=freak(a,b)
– will execute what is defined in the function between the lines [q,z]=freak(a,b) and endfunction
that is, it will calculate x and y:
x=4+7=11
y=4-7=-3
It will then execute 2 display instructions
disp(q)// here will substitute x=11 for q
disp(z)// here with z substitute y=-3
As a result of the function, the following text was created
11.
-4.
Chapter 6.4 A non-simple function with 3 inputs and 2 outputs
It is, for example, a function that calculates the roots x1,x2 of the quadratic equation
a*x²+b*x+c=0
P6-3 program

```clear,clc;
function [p,q]=square(a,b,c)
d=b^2-4*a*c
p=(-b+sqrt(d))/(2*a)
q=(-b-sqrt(d))/(2*a)
if d>0 then
disp('real and different roots')
elseif d==0 then
disp('real and identical roots')
elseif d<0 then
disp('complex roots')
end
endfunction
[x1,x2]=square(1,-5,6)
disp('x1=',x1)
disp('x2=',x2)```

The function [x1,x2]=square(1,-5,6) looks for the roots of the equation 1*x²-5*x+6=0
After executing the program, a text was created
“real and different roots”
x1=”
3.
“x2=”
2.
Change the function in the program to [x1,x2]=quare(1,-4,+4) which looks for the roots of the equation 1*x²-4*x+4=0
After executing the program, a text was created
“real and identical elements”

Change the function in the program to [x1,x2]=square(1,-2,+5) which looks for the roots of the equation 1*x²-2*x+5=0
After executing the program, a text was created
“complex elements”
“x1=”
1.+ 2.i
“x2=”
1. – 2.i
The program calculated that there are 2 complex elements. And here I was pleasantly surprised. Scilab, as a specialized calculation program, after calculating delta as a negative number, made the decision that it was dealing with complex numbers. I don’t know if C++ would do that too. Wouldn’t he have to rely on libraries?

Scroll to Top