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

 Respuesta aceptada

Torsten
Torsten el 6 de Abr. de 2023
Editada: Torsten el 6 de Abr. de 2023
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 comentarios

Alice K
Alice K el 6 de Abr. de 2023
That looks great thank you, but can you explain it please I don't fully understand what gets plotted.
Torsten
Torsten el 6 de Abr. de 2023
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).
Alice K
Alice K el 6 de Abr. de 2023
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 el 6 de Abr. de 2023
Editada: Torsten el 6 de Abr. de 2023
What ? Green is nature - you don't like green ?
plot(x,F(:,1),'r',x,F(:,10),'b',...
r = red
b = blue
...
Alice K
Alice K el 6 de Abr. de 2023
Brilliant thank you, not the biggest fan of green
Alice K
Alice K el 6 de Abr. de 2023
Editada: Alice K el 6 de Abr. de 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.
Torsten
Torsten el 6 de Abr. de 2023
Editada: Torsten el 6 de Abr. de 2023
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));
Alice K
Alice K el 6 de Abr. de 2023
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)
Torsten
Torsten el 6 de Abr. de 2023
Editada: Torsten el 6 de Abr. de 2023
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.
Alice K
Alice K el 6 de Abr. de 2023
amazing, thank you so much for all you help

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 6 de Abr. de 2023

Comentada:

el 6 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by