Matlab for Loop of two changing variables

Click For Summary

Discussion Overview

The discussion centers around a MATLAB coding problem involving nested loops with two changing variables, specifically how to calculate and store values of a variable named sigmar for different values of r and teta. The scope includes programming techniques in MATLAB, particularly focusing on data storage and plotting in polar coordinates.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • Adeeyo seeks assistance in writing MATLAB code to calculate sigmar for varying values of r and teta without overwriting previous results.
  • Some participants suggest using cell arrays or structures to store the calculated values of sigmar for each combination of r and teta.
  • Adeeyo confirms the need to store all calculated sigmar values for plotting later.
  • Adeeyo expresses interest in creating a polar plot of r versus sigmar for the calculated values.

Areas of Agreement / Disagreement

Participants generally agree on the need to store values without overwriting, but specific methods for implementation are not fully resolved, and no consensus on the best approach is reached.

Contextual Notes

There are limitations regarding the clarity of how to implement the storage of values and the specifics of the polar plot code, which remain unresolved in the discussion.

adeeyo
Messages
20
Reaction score
0
Hi,
Please I need your assistance on this MATLAB code. I will really appreciate it.

R=0.492; Pw=0.3708;

for r=1:5;

for teta=10:10:360;


sigmar=(1-(R.^2./r.^2))+(1-((4.*R.^2)./r.^2)+((3.*R.^4)./r.^4)).*cos(2.*teta)+(1-((4.*R.^2)./r.^2)+((3.*R.^4)./r.^4)).*sin(2.*teta)+Pw.*(R.^2./r.^2);


end

end

The only constraint is that for each time r changes, I want the loop to calculate value for sigmar with teta going form 10-360 with increament of 10 (teta=10:10:360).
To make it clearer, What I want is that, I want the code to take r=1, calculate 36 values for sigmar using teta=10:10:360, then take r=2, calculate 36 values for sigmar using teta=10:10:360, then take r=3, calculate 36 values for sigmar using teta=10:10:360, then take r=4, calculate 36 values for sigmar using teta=10:10:360, then take r=5,calculate 36 values for sigmar using teta=10:10:360.

Thanks for your usual assistance


Adeeyo
 
Physics news on Phys.org
Are you asking how to store the variables so that it doesn't get overwritten each time? If so, you could probably do that using cell arrays/structures/arrays

For example:
rndx = 1;

for r=1:5;
tetandx = 1;
for teta=10:10:360;


sigmar= ...
data(1,tetandx,rndx) = sigmar;

tetandx = tetandx + 1;
end
rndx = rnd + 1;
end
 
Hi Gringo,

Thanks for your quick response.
Yes. What I want is that I want calculated values of sigmar to be stored and not over-written at each value of r and teta=10:10:360.

Once again thanks

Adeeyo
 
Hi Gringo,

I want to make polar plot of the value of r and sigmar. polar(r,sigmar). Please how do I write the code so that I will have at each r (from r=1 to r=5) all the sigmar values corresponding to 36 teta values plotted in polar coordinate.

I am very grateful



Adeeyo
 
adeeyo said:
Hi Gringo,

I want to make polar plot of the value of r and sigmar. polar(r,sigmar). Please how do I write the code so that I will have at each r (from r=1 to r=5) all the sigmar values corresponding to 36 teta values plotted in polar coordinate.

I am very grateful



Adeeyo

Hey Adeeyo and welcome to the forums.

You should take a look at this for the general idea:

http://stackoverflow.com/questions/466972/array-of-matrices-in-matlab
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K