Plotting a summation with two variables

Hi All,
I need to plot a summation but I keep getting a blank plot,
Any help you can give would be great.
Here is my summation:
Here is the code I have tried:
t = linspace(0,0.01,1);
x = linspace(0,0.01,1);
n = 100;
for k = 0:1:n
y = (6/(pi+6*k*pi))*sin(k*x)*exp((-(pi/6+k*pi)^2)*t);
end
plot(v,t)
Thanks
Alice

 Accepted Answer

t = linspace(0,0.1,100);
x = linspace(0,1,100).';
n = 1000;
f = @(t) sum(6./(pi+6*(0:n)*pi).*sin(x.*(0:n)).*exp(-(pi/6+(0:n)*pi).^2*t),2);
for j = 1:numel(t)
F(:,j) = f(t(j));
end
figure(1)
plot(x,[F(:,1),F(:,10),F(:,15),F(:,20),F(:,100)])
figure(2)
plot(t,[F(1,:);F(10,:);F(15,:);F(20,:);F(100,:)])

10 Comments

That looks great thank you, but can you explain it please I don't fully understand what gets plotted.
Figure 1 shows y over x for t = t(1), t(10), t(15), t(20), t(100).
Figure 2 shows y over t for x = x(1), x(10), x(15), x(20), x(100).
Okay, that makes sense, just one last question is there a way to set the line colours? I can get the lines to be all of one colour but not different, I need the colours not to contain green.
Torsten
Torsten on 6 Apr 2023
Edited: Torsten on 6 Apr 2023
What ? Green is nature - you don't like green ?
plot(x,F(:,1),'r',x,F(:,10),'b',...
r = red
b = blue
...
Brilliant thank you, not the biggest fan of green
Alice K
Alice K on 6 Apr 2023
Edited: Alice K on 6 Apr 2023
Sorry two last questions (i think)
is this line f = @(t) sum(6./(pi+6*(0:n)*pi).*sin(x.*(0:n)).*exp(-(pi/6+(0:n)*pi).^2*t),2);
what is the 2 aftter the comma at the end, and how would I get it to plot when t = 0, it is just plotting a blank plot.
Thank you for all your help.
Summation of the matrix
6./(pi+6*(0:n)*pi).*sin(x.*(0:n)).*exp(-(pi/6+(0:n)*pi).^2*t)
over its columns (sum(.,1) would be summation over its rows).
For a better understanding, I think it would be a good exercise if you tried to solve this problem by a threefold loop:
for i = 1:numel(x)
xv = x(i);
for j = 1:numel(t)
tv = t(j);
F(i,j) = 0.0;
for k = 0:n
F(i,j) = F(i,j) + 6/(pi+6*k*pi)*sin(xv*k)*exp(-(pi/6+k*pi)^2*tv);
end
end
end
I guess this is somehow what you had in mind with your code.
Instead of the loop
for j = 1:numel(t)
F(:,j) = f(t(j));
end
in the above code, you can also directly use
F = cell2mat(arrayfun(@(t)f(t),t,'UniformOutput',0));
I am a bit lost, where would I put this code or what does it replace?
F = cell2mat(arrayfun(@(t)f(t),t,'UniformOutput',0));
and would I do the same if i wanted to plot this:
g = @(t) sum(6./(pi+6*(0:n)*pi).*sin(x.*(pi./6+(0:n)*pi)),2)
You can use
t = linspace(0,0.1,100);
x = linspace(0,1,100).';
n = 1000;
f = @(t) sum(6./(pi+6*(0:n)*pi).*sin(x.*(0:n)).*exp(-(pi/6+(0:n)*pi).^2*t),2);
for j = 1:numel(t)
F(:,j) = f(t(j));
end
or
t = linspace(0,0.1,100);
x = linspace(0,1,100).';
n = 1000;
f = @(t) sum(6./(pi+6*(0:n)*pi).*sin(x.*(0:n)).*exp(-(pi/6+(0:n)*pi).^2*t),2);
F = cell2mat(arrayfun(@(t)f(t),t,'UniformOutput',0));
or
t = linspace(0,0.1,100);
x = linspace(0,1,100).';
n = 1000;
F = zeros(numel(x),numel(t));
for i = 1:numel(x)
xv = x(i);
for j = 1:numel(t)
tv = t(j);
for k = 0:n
F(i,j) = F(i,j) + 6/(pi+6*k*pi)*sin(xv*k)*exp(-(pi/6+k*pi)^2*tv);
end
end
end
F should be the same for all three codes, but I think for beginners, the last code is the easiest to understand.
amazing, thank you so much for all you help

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 6 Apr 2023

Commented:

on 6 Apr 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!