Effacer les filtres
Effacer les filtres

Double for loop is a problem ??? Any help, I can't find the right results when using the second for loop!!!!!

1 vue (au cours des 30 derniers jours)
Hello, I have to use two for loops in my code to calculate the matrix c, xp and yp. The first one concerning the parameter rr and the second for xxb.
To cheek my loops if works well, I compare the values of C and xp for yp when I use only the foo loop for rr for fixed values of xxp. But for some values of xxb I don't find the right results. For exemple , for xxb=0.05, and for 0.1, and for 0.15 the resuls not the same for the two cases.
Any Idea??? Thank you in advance,
Adam
Here is below my code:
xb=-5:0.05:5;
xxb=xb';% column vector
dr=0.01;
r=0:dr:5;
rr=r;% row vector
R=0.05;
for j=1:length(xxb);
for m=1:length(rr);
a(j)=2.*xxb(j);
c(j,m)=xxb(j).^2+rr(m).^2-R.^2;
xp(j,m)=c(j,m)./a(j);
yp(j,m)=R.^2-((((2.*c(j,m))-(a(j).^2))./(2.*a(j))).^2);
end
end
the end of code:
For exemple
using two for loop For the firt values of arrow of index 102 corresponding to the xxb=0.05:
-1,82145964977565e-17
9,99999999999816e-05
0,000399999999999982
0,000899999999999982
0,00159999999999998
0,00249999999999998
0,00359999999999998
0,00489999999999998
..........
using one for loop for xxb=0.05
0
9,99999999999998e-05
0,000400000000000000
0,000900000000000000
0,00160000000000000
0,00250000000000000
0,00360000000000000
..................
  3 commentaires
Bård Skaflestad
Bård Skaflestad le 18 Août 2012
Also, you may want to write your code using vectorised statements, e.g., something like this:
xb = -5:0.05:5;
dr = 0.01;
r = 0:dr:5;
R = 0.05;
[xxb, rr] = ndgrid(xb, r);
a = 2 .* xxb;
c = xxb.^2 + rr.^2 - R.^2;
xp = c ./ a;
yp = R.^2 - (((2.*c - a.^2) ./ (2 .* a))).^2;
which, in addition to being easier to read, is also likely to run more efficiently--at least if the sizes of xb or r do not increase too much.
adam
adam le 21 Août 2012
Thank you . I posted all my code in new way under the below question if you can help me .
Condition if on the elements of matrix in two For loop doesn't work

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by