Plotting a summation with two variables
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1347769/image.png)
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
0 Kommentare
Akzeptierte Antwort
Torsten
am 6 Apr. 2023
Bearbeitet: Torsten
am 6 Apr. 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 Kommentare
Torsten
am 6 Apr. 2023
Bearbeitet: Torsten
am 6 Apr. 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.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!