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

조회 수: 1 (최근 30일)
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

채택된 답변

Geoff Hayes
Geoff Hayes 2017년 12월 11일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by