How to plot a graph with a for loop
Ältere Kommentare anzeigen
I have a question where I'm required to create a for loop and then use it to plot a graph. I have n = 0,1,2 ... 10 and need to plot cos(n*pi*x/2) for this. My current code looks like this as I'm trying to store the outputs of the for loop in an array
syms x n b a
nvalues = zeros(1, 10);
for n = 1:11
S = cos((n-1)*pi*x*(1/2));
nvalues(n) = S;
end
nvalues
1 Kommentar
Mathieu NOE
am 20 Jan. 2021
hello
simply add to your code - last line :
figure, plot(1:max(n),nvalues)
Akzeptierte Antwort
Weitere Antworten (1)
Bram Schroeders
am 20 Jan. 2021
Datapoints = 1000;
x = linspace(0,2*pi,Datapoints);
S = zeros(size(x));
hold all
for n = 1:11
S = cos((n-1)*pi*x*(1/2));
plot(x,S);
end
This should do the trick, you won't need the symbolic toolbox for this.
You can also save all the data by making a matrix out of S.
1 Kommentar
Yuvraj Bhagotra
am 20 Jan. 2021
Kategorien
Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
