Multiple plotting issue?

6 views (last 30 days)
Andi
Andi on 27 Jan 2022
Commented: Andi on 27 Jan 2022
Hi everyone,
As per my script i expect an oputput of three plots but I get only one plot.
May someone suggets how i can fix this.
As per code there shoudl be three plots for t=1, 2 and 3, but i get only one plot for t=3.
clear all
clc
xs=8;
v=3;
L=20;
ta=0.2;
t=1:1:3;
for k=1:length(t)
n=1:1:40;
x=0:0.1:20;
omga = zeros(1,length(n));
F = zeros(1,length(n));
u = zeros(length(n),length(x));
for j=1:length(n)
F(j)=exp(-((n(j)*pi*v*ta/L).^2)/4);
u(j,:)=sin(n(j)*pi*xs/L)*F(j)*sin(n(j)*pi*x/L)*cos(n(j)*pi*v/L*t(k)); % no loop needed
end
us = sum(u);
plot(x,us)
end

Accepted Answer

Ankit
Ankit on 27 Jan 2022
Edited: Ankit on 27 Jan 2022
Few recommendations to your code:
  • try to define all the variable before loop
  • don't use clear all in your code. Calling clear all decreases code performance, and is usually unnecessary. To clear one or more specific variables from the current workspace use clearvars instead
clc
xs=8;
v=3;
L=20;
ta=0.2;
t=1:1:3;
n=1:1:40;
x=0:0.1:20;
us = zeros(1,length(x));
omga = zeros(1,length(n));
F = zeros(1,length(n));
u = zeros(length(n),length(x));
for k=1:length(t)
for j=1:length(n)
F(j)=exp(-((n(j)*pi*v*ta/L).^2)/4);
u(j,:)=sin(n(j)*pi*xs/L)*F(j)*sin(n(j)*pi*x/L)*cos(n(j)*pi*v/L*t(k)); % no loop needed
end
us(k,:) = sum(u);
end
plot(x,us);
  3 Comments
Ankit
Ankit on 27 Jan 2022
what do you mean by video of these plots? you mean how one by one all your three plots are plotted?
here you can see some options: Get figures and use them to build a video.avi - (mathworks.com). You can also search on MATLAB File Exchange
Andi
Andi on 27 Jan 2022
Exactly, I want to build a video of all these plot. Thanks for sharing

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!