Search code examples
matlabfactorialloglog

how to draw with matlab two seprate graphs with loglog?


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);

Solution

  • 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.