Why am I not getting multiple graphs after iteration of the following equation, I am using two 'for' loops

1 Ansicht (letzte 30 Tage)
Equation: F"= G(G^2+lambda*gamma^2)/(G^2+gamma^2)
for different values of parameter 'G' and parameter 'gamma' are calculated and plotted but I not getting those graphs
here's the code that I've used:
global lambda gama
lambda=1;
qq=[-0.6777 -0.7712 -0.8737];
pp=[0.2:0.2:8];
for i=1:numel(pp);
for G=qq;
gama=pp(i);
Fss(i)= G*(G^2+gama^2)/(G^2+lambda*gama^2)
end
end
plot(pp,Fss);hold on

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 11 Dez. 2017
naygarp - the problem is that you are overwriting your values in FSS found in the previous loop with those values in the current loop
Fss(i)= G*(G^2+gama^2)/(G^2+lambda*gama^2)
Your loops are iterating over i and G and so you need to take that into account when updating FSS. Consider the following instead
for i=1:numel(pp);
k = 1;
for G=qq;
gama=pp(i);
Fss(i,k)= G*(G^2+gama^2)/(G^2+lambda*gama^2);
k = k + 1;
end
end
Note that FSS will now be a 40x3 matrix where each column is set for each iteration of the outer loop.
As an aside, you may want to consider pre-allocating (or pre-sizing) your matrix before iterating. Also, reconsider your use of global variables - are they really necessary? If you create a function (rather than a script) your function can return any variables/values that are needed outside of it.

Weitere Antworten (0)

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by