1
votes

I am trying to create a step function in scilab, that jumps between two values {28,36} and stays on each for 5 units of 'i'.
Here is my code;

    for i=1:25;
        if pmodulo(i,5)==0
             if a==28
                a=36
             else
                a=28
             end
        end
    end
    plot(i,a)

can some one please tell me what am missing, cause i keep getting a plot screen with no graph and my i-axis in the plot screen has values from 20 to 25 instead of 1 to 25, which tells me am doing something wrong

1

1 Answers

3
votes

Your code

There are some problems with your Scilab code given. The biggest problem being that i and a are only doubles on the moment of trying to plot them. During the for loop i gets reassigned continuously. So when the for-loop is exited, its value is 25.

The same applies to a. In the first iteration it is completely unknown. So the code will fail for sure. If you add a declaration above the for-loop initializing the value, it would at least exist. But then it will continuously get reassigned either 36 or 28.

At the end of the for-loop you would end up with i=25 and a=36.

Step function

To plot a step function in Scilab. Scilab provides the plot2d2 functionality. Look at the docs for more info, but for your example, below an extremely verbose piece of example code.

start_X = 0;
step_X = 5;
end_X = 24;

high=36;
low=28;

X = [start_X:step_X:end_X];
Y = [low,high,low,high,low];

plot2d2(X,Y);

Step function

Getting started with SciLab

DuckDuckGo provides a lot of getting started tutorials, which may give you some more insight in SciLab.