plotting two graphs in matlab

205 Views Asked by At

it's been 5 hours and I'm trying to plot two graphs in matlab but in vain, here is my code

x = -pi:.1:pi;

z =(1/(2*pi))*exp(-power(x,2)/2);

y=((n.^(n/2))/fact(n-1))*((x + sqrt(n)).^(n-1))*exp(-sqrt(n)*(x + sqrt(n)));

plot(x,z,'color','r'); hold on;

plot(x,y,'color','b');

if there is an other page where I can ask this question please tell me !

2

There are 2 best solutions below

2
On BEST ANSWER

For now, I can only see that you forgot to define n. So, either you choose n=... or you do a for loop and vary n from 1..N and define the upper bound N.

Ok. Say n=10. Your code will work like this:

 n = 10;

 x = -pi:.1:pi;

 z =(1/(2*pi))*exp(-power(x,2)/2);

 y=((n.^(n/2))/factorial(n-1)).*((x + sqrt(n)).^(n-1)).*exp(-sqrt(n)*(x + sqrt(n)));

 plot(x,z,'color','r'); hold on;

 plot(x,y,'color','b');

First, n! is factorial not fact in MATLAB. Also, you forgot .* multiplication to do vector one by one multiplication.

The above code should work.

enter image description here

1
On

You need to use Hadamard multiplication rather than matrix:

y=((n.^(n/2))/fact(n-1))*((x + sqrt(n)).^(n-1)) .* exp(-sqrt(n)*(x + sqrt(n)));

When you see the following error,

Error using  * 
Inner matrix dimensions must agree.

all you have to do is look at the sizes of the arrays to the left and right of each * operator. You will then quickly find the source of the error.