3
votes

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).
3

3 Answers

4
votes

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

1
votes

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.

0
votes

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