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

 채택된 답변

Torsten
Torsten 2023년 4월 6일
편집: Torsten 2023년 4월 6일
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

Alice K
Alice K 2023년 4월 6일
That looks great thank you, but can you explain it please I don't fully understand what gets plotted.
Torsten
Torsten 2023년 4월 6일
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 2023년 4월 6일
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 2023년 4월 6일
편집: Torsten 2023년 4월 6일
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 2023년 4월 6일
Brilliant thank you, not the biggest fan of green
Alice K
Alice K 2023년 4월 6일
편집: Alice K 2023년 4월 6일
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 2023년 4월 6일
편집: Torsten 2023년 4월 6일
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 2023년 4월 6일
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 2023년 4월 6일
편집: Torsten 2023년 4월 6일
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 2023년 4월 6일
amazing, thank you so much for all you help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 4월 6일

댓글:

2023년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by