3

How would you plot these in SciLab or MatLab? I am new to these and have no idea how the software works. Please help.

$Plot following functions with different colors in Scilab or MatLab
–   f2(x) = logn
–   f3(x) = n
–   f4(x) = nlogn
–   f5(x) = n2
–   f6(x) = nj (j > 2)
–   f7(x) = cn (c > 1)
–   f8(x) = n!

where x = linspace(1, 50, 50).

4 Answers 4

4

Well, a lot of these are built-in functions. For example

>> x = linspace(1,50,50);
>> plot(x,log(x))
>> plot(x,x)
>> plot(x,x.*log(x))
>> plot(x,x.^2)

I don't know what nj (j > 2) and cn (c > 1) are supposed to mean.

For the last one, you should look at the function factorial.

It's not clear from the context whether you're supposed to plot them on different graphs or all on the same graph. If all on the same graph, then you can use

>> hold on;

to freeze the current axes - that means that any new lines will get drawn on top of the old ones, instead of being drawn on a fresh set of axes.

In Matlab (and probably in Scilab) you can supply a "line spec" argument to the plot function, which tells it what color and style to draw the line in. For example,

>> figure
>> hold on
>> plot(x,log(x),'b')
>> plot(x,x/10,'r')
>> plot(x,x.^2/1000,'g')

Tells Matlab to plot the function f(x)=log(x) in blue, f(x)=x/10 in red and f(x)=x^2/1000 in green, which results in this plot:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

I can't comment or upvote yet but I'd add to Chris Taylor's answer that in Scilab the hold on and hold off convention isn't used. All plot commands output to the current axes, which are 'held on' all the time. If you want to generate a new figure or change the current axes you can use figure(n), where n can be any (nonconsecutive) positive integer - just a label really.

See also clf(n), gcf() and gca() - Scilab's figure handling differs quite a bit from Matlab's, though the matplotlib ATOMS module goes some way towards making Scilab look and behave more like Matlab.

Comments

0

In Scilab, it will be

x = 1:50;
clf
plot("ll", x,log, x,x, x,x.*log(x), x,x.^2)
gca().sub_ticks(2) = 8;
xgrid(color("grey"))
legend("$"+["ln(x)", "x", "x.ln(x)", "x^2"]+"$", "in_upper_left")

enter image description here

Comments

0

Important General Idea

To be sure to know well the basic principles of a scilab / matlab function handling:

A function is a relation between groups of numbers. For each x value you have up to one y value. For an example, y=3x has domain in any real number and ranges to any real number, which means x=1 has y=3, x=0.002 has y=0.006, etc.

But in scilab and matlab, we need to define each data point for which the program will relate x and y, i.e. we work with discrete data, which you are doing well with linspace. So we have a finite group of x values for which we intend to get y values. That is important to keep in mind. This means if we specify x as the linspace you mentioned, x=0.1 does not exist, and the "resolution" or "differential step" for x is 1 (x varies 1 by 1 each value).

Now, if you are in a hurry, go for the codes I put in each section.

Help command

Type in scilab terminal the command:

help functionName

This, combined with googling "scilab funcionName" or "scilab taskDefinition" will super expand your self-reliance into learning matlab or scilab. If you're in a hurry using help, go for the code examples available in the window it opens. Suggested commands for you:

help linspace
help plot
help log
help factorial
help for
help grid

Plotting

for f2(x) = logn I've understand you intend to plot a log(n) function = f2(n). To declare a number of values for x axis in scilab, the below solutions will return the same line matrix of discrete data:

n = linspace(1,50,50);

or

x = 1 : 1 : 50;

You may remove the ; at the end of the command to check it's return.

Now, into plotting:

plot(log(n))

The code above uses the plot function passing the returned values of another function as parameter (log), which itself receives as parameter the variable n you just declared (if you did the linspace command line exemplified). If it fails, maybe you declared x instead of n to receive the linspace or range of values.

I suggest you also try the code:

close  //closes other opened graph window. All text after // is a comment and won't run
figure; //creates a graph window
subplot(2,1,1) // creates and selects an area in the figure handler
title("f(n) = log n")
plot(log(n)); //draws graph into available selected area. Auto-opens figure if necessary
subplot(2,1,2) //selects second area of blocks 2 rows vs 1 column to plot
title("f(n) = n")
plot(n);

The above code solves for f2 and f3, I suppose. Simpler alternative:

figure,
a=log(n);
plot(a)
figure,
plot(n)

For f4(n) = nlogn you can't multiply the row matrices n and log(n), so we need to create a new line matrix and populate with the result of n*log(n).

close  //use this as much as you please, or manually close other windows
n=1:1:50;
a=log(n);
b=n;
f4=zeros(1,50)  //I took the ; so you see the inintial result
for i=1:1:50  //for each i value from 1 to 50, incrementing i by 1 each time
    f4(i) = a(i) * b(i);  //each i data point of f4 becomes i data point from a multiplied by i data point from b
end
plot(f4)

For f6= nj (j > 2) you didn't specify j, so for an example if j(x) = sin(x), try

close
x = 2:1:50;  //defines range for x > 2
j = sin(x);  //defines j in same range as sin(x values)
figure,
title("f6(n) = n*j,  j=sin(n),  {f6, n} > 2 ")
f6 = zeros(1,49);
for i=1:1:49
    f6(i) = x(i)*j(i);
end
plot(f6)
xlabel("n")  //use help xlabel if curious
ylabel("f6(n)")

Same idea for f7.

For f8 you might use

close
x = linspace(1, 50, 50);
f8 = factorial(x);
plot(f8)

You might need a zoom there, or reducing linspace range, as factorial values raise very fast.

Extra Tip

There is decent chance some known math functions like factorial are defined in scilab. I didn't know that function, though, so I googled "scilab factorial". Help is also available to try out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.