0

i am trieng to draw with matlab the two functions :

y1=ln(n!) and y2=ln(n)*n when n is a vector like this : n=1:100 i want to draw both of these functions in the same graph , and then draw another function : ((y2-y1)/y1) on a different graph .. i did the following but it only shows me the first graph with only one function , any help of what i am doing wrong ? thanks.

n=1:100;
format long
n_factorial=factorial(n);
y1 =log(n_factorial);
figure;
loglog(n,y1,'b');
hold on;
y2=(n*(log(n)'));
loglog(n,y2,'r');

y3=((y2-y1)/y1);
loglog(n,y3);
james
  • 23
  • 7
  • 1
    Reposting since I forgot to escape out `*` verus `.*` but basically this is your issue, nothing to do with the plotting, you need to understand the difference between matrix multiplication and element-wise multiplication in Matlab: uk.mathworks.com/help/matlab/ref/mtimes.html and uk.mathworks.com/help/matlab/ref/times.html – nkjt May 06 '18 at 13:17
  • but i did that and i still get one graph :/ – james May 06 '18 at 15:41
  • When you say different graphs, do you mean within the same figure? If so, check out the function `subplot` – verbatross May 07 '18 at 02:30

1 Answers1

0

There is two different method you can use to draw multiple graphs.

If you want to draw the two different graph on two different window, you should add the line :

figure;

each time you want to plot on a new window. In your code, you should now have

figure;
loglog(n,y1,'b'); hold on;
loglog(n,y2,'r');

figure;
loglog(n,y3);

If you want to draw two different graph on the same 'figure', you should use the command subplot, like that:

subplot(2,1,1);
loglog(n,y1,'b'); hold on;
loglog(n,y2,'r');

subplot(2,1,2);
loglog(n,y3);

which basically divide your window's area between 2 lines and one row, and give to each position an index (in this case, 1 & 2) that you specify with the third parameter of the subplot command.

Also, I think there is a mistake in the code you posted for the dimensions of your vectors. You should verify what you wanna plot.