Clear Filters
Clear Filters

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

3 views (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Dec 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.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!