How do I plot a graph from a code that is is using a function and looping feature
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Christopher Raymond
le 10 Déc 2018
Modifié(e) : Mark Sherstan
le 10 Déc 2018
I need to plot the below equation with "S" as a function of n. The code I wrote (below) isnt working so I assume that i have some sort of syntax issue ?
NN = [0:1:10];
for i = 1:length(NN)
S(i) = S(i) + (1/(2^NN(i)));
NN(i) = NN(i) +1;
end
plot(NN,S,'k-')
0 commentaires
Réponse acceptée
Mark Sherstan
le 10 Déc 2018
Modifié(e) : Mark Sherstan
le 10 Déc 2018
You are redeffining NN unessecarily and the first instance of S(i) is not defined. Consider preallocationg and changing your code to the folowing.
NN = [0:1:10];
S = zeros(length(NN),1);
for i = 1:length(NN)
S(i) = S(i) + (1/(2^NN(i)));
end
plot(NN,S,'k-')
Note: Your equation doesent make sense as S(i) is a function of itself at each step, S(i) = S(i).
0 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!