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
採用された回答
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
2023 年 4 月 6 日
That looks great thank you, but can you explain it please I don't fully understand what gets plotted.
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
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.
What ? Green is nature - you don't like green ?
plot(x,F(:,1),'r',x,F(:,10),'b',...
r = red
b = blue
...
Alice K
2023 年 4 月 6 日
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));
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)
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
2023 年 4 月 6 日
amazing, thank you so much for all you help
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
