# loops in matlab, plotting matrix elements

• Apr 9th 2011, 07:51 AM
Marine
loops in matlab, plotting matrix elements
Hi all!

I want to perform the following summation numerically:

$r(x,t)=\sum_{p=-N}^N\cos(pt)\cos(px)e^{-p^2}$

where t and x are parameters with respect to which I will subsequently plot the results

x is between -C and C, and t is between -K and K for some positive constants C, K

I would like to get a matrix r, whose columns are the summation values for fixed t and whose rows the summation values for fixed x.

Here's my attempt for K = C = 10 and N = 5, but it fails :( - I only get a single number r.

Quote:

for t = -10:0.1:10;
for x = -10:0.1:10;
for p = -5:1:5;
A = 2.*cos(p*t).*cos(p*x).*exp(-p.^2);
r = sum(A);
end;
end;
end;
The plots I want are for r(t), r(x) the other variable being fixed, respectively, and a polar plot for r(x,t). Does anyone know how to read off and plot a column/row from a matrix?

Thanks a lot!
marin
• Apr 10th 2011, 09:50 AM
Marine
here's an improvement to the above code:

Quote:

%set initial values for the parameter j%
i=1;
j=1;
for t = 0:.1:10; %set time interval t%
j = 1;

for x = -10:.1:10; %set line interval x%

p = 0:1:100; %define a summation prameter p%
A = 2.*cos(p*t).*cos(p*x).*exp(-p.^2); %define the sumamtions vector A%
s(i,j) = sum(A); %perform summation for fixed x and t and define the (i,j)-component of matrix s%

j=j+1; %increase j by 1%

end;
i=i+1; %increase i by 1%
end;
pcolor(s)
I'm still unsure about it though... Can anyone tell me if it's ok?