Matlab for Loop of two changing variables

In summary: ZCjwWf3wC&rank=2Or you could use this code snippet:% Polar plotplot(rand(1,5),rand(1,5),'r')% Titletitle('Polar plot of ' + str(r) + ' and ' + str(sigmar))
  • #1
adeeyo
20
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
  • #2
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
 
  • #3
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
 
  • #4
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
 
  • #5
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
 

What is a for loop in Matlab?

A for loop in Matlab is a programming construct that allows you to execute a block of code repeatedly for a specified number of times. It is commonly used to iterate through an array or perform a task on multiple values.

Can a for loop have two changing variables in Matlab?

Yes, a for loop in Matlab can have two changing variables. This allows you to loop through a two-dimensional array or perform a task on two different arrays simultaneously.

How do you declare two changing variables in a for loop in Matlab?

To declare two changing variables in a for loop in Matlab, you can use the following syntax:

for variable1 = value1 : value2 : step1, variable2 = value3 : value4 : step2

where variable1 and variable2 are the two changing variables, value1 and value3 are the starting values, value2 and value4 are the ending values, and step1 and step2 are the increments.

What is the difference between a nested for loop and a for loop with two changing variables in Matlab?

A nested for loop is a loop inside another loop, while a for loop with two changing variables is a single loop with two changing variables. In the case of a nested for loop, the inner loop will be executed completely for each iteration of the outer loop, while in a for loop with two changing variables, both variables will change simultaneously for each iteration.

Can you provide an example of a for loop with two changing variables in Matlab?

Yes, here is an example of a for loop with two changing variables in Matlab that prints out the multiplication table from 1 to 10:

for i = 1:10, j = 1:10, disp(i*j)

This will result in the following output:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
950
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
221
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top