Plotting a summation with two variables
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
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
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.
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
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
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
