How to iterate it Please help me

Hello!! I need help!! For Iteration
Here is my first iteration:
S12t=6+2*1i;
S01t=10+5*1i;
U0=110;
U1=110;
U2=110;
R12 = 16.2+24*1i;
U0i=115;
R01= 15.75+31.2*1i;
dx01=106.125;
dx12=81;
U20=10;
e=0.01;
dQ12=dx12*U2.^2*10.^-6;
S12i=S12t-complex(0,dQ12);
DS12 =((real(S12i).^2+imag(S12i).^2)/U2.^2)*R12;
S12ii=DS12+S12i;
S12e=S12ii-complex(0,dQ12);
S01i=S12e+S01t;
dQ01=dx01*U1.^2*10.^-6;
S01ii=S01i-complex(0,dQ01);
DS01=((real(S01ii).^2+imag(S01ii).^2)/U1.^2)*R01;
dQ01i=dx01*U0i.^2*10.^-6;
S01ii2=S01ii+DS01;
S01iii=S01ii2-complex(0,dQ01i);
dU01i=(real(S01ii2)*real(R01)+imag(S01ii2)*imag(R01))./U0i;
dU01ii=(real(S01ii2)*imag(R01)-imag(S01ii2)*real(R01))./U0i;
U1=sqrt((U0i-dU01i).^2+dU01ii.^2);
dU12i=(real(S12ii)*real(R12)+imag(S12ii)*imag(R12))./U1;
dU12ii=(real(S12ii)*imag(R12)-imag(S12ii)*real(R12))./U1;
U2=sqrt((U1-dU12i).^2+dU12ii.^2);
And i want it continue to next iteration.
continue it if U0-(max of U1 U2)>e
U1 U2 will be changed last 2 value in next iteration
end it if U0-(max of U1 U2)<e
How can i put the if continue (while, return, end )
I can't understand I tried so many times, but still error message "usage might invalid Matlab syntax" appeared.
Just help me tell where can i put that statements If, while, continue in Long Function. Please help me!!
Thank You!!

 Réponse acceptée

Walter Roberson
Walter Roberson le 26 Mai 2013

0 votes

I have not studied the code thoroughly but it appears to me that you do not assign new values to any variables you used previously. Unless at least one input variable gets a new value, then when you iterate the results would have to come out exactly the same and you would iterate forever. Something needs to change inside the loop before you can hope to have your iteration terminate.
Your general code structure will look something like this:
Initialize your variables
while true %"true" is literal here, not an example variable name
Do some calculation
if your termination condition holds
break; %"break" is literal, a MATLAB command
end
%you only get here if you did not break out, so if the termination
%condition did not hold
end %of while loop. If the termination condition did not hold, loop back

7 commentaires

Light
Light le 26 Mai 2013
Oops! I forgot it. I edited it again continue it if U0-(max of U1 U2)>e U1 U2 will be changed last 2 value in next iteration Thank you for your big help! You are fast ^_^ Please check it one more time Please!
Light
Light le 26 Mai 2013
How can i show the iteration number :D
The "while true" would go before
dQ12=dx12*U2.^2*10.^-6;
as that is the first statement that needs the updated U1 or U2.
In your initialization, say
iteration_number = 0;
and then right after the "while true"
iteration_number = iteration_number + 1;
then display iteration_number anywhere before the end of the loop.
fprintf('Starting iteration #%d\n', iteration_number);
Light
Light le 26 Mai 2013
Modifié(e) : Light le 26 Mai 2013
Is that right?
while U0-U2>e;
iteration_number = iteration_number + 1;
dQ12=dx12*U2.^2*10.^-6;
....
....
U2=sqrt((U1-dU12i).^2+dU12ii.^2);
fprintf('Starting iteration #%d\n', iteration_number);
end
No error message, but No any results. "while true"-'s true didn't changed to blue. ;-( . and can i give iteration number. Sorry for my bad knowledge and thank you for your tender mind.
You start out with U1 and U2 being the same, so their difference is 0, which is less than e, so your loop would not run if you use that "while".
S12t=6+2*1i;
S01t=10+5*1i;
U0=110;
U1=110;
U2=110;
R12 = 16.2+24*1i;
U0i=115;
R01= 15.75+31.2*1i;
dx01=106.125;
dx12=81;
U20=10;
e=0.01;
iteration_number = 0;
while true
iteration_number = iteration_number + 1;
fprintf('Starting iteration #%d\n', iteration_number);
dQ12=dx12*U2.^2*10.^-6;
S12i=S12t-complex(0,dQ12);
DS12 =((real(S12i).^2+imag(S12i).^2)/U2.^2)*R12;
S12ii=DS12+S12i;
S12e=S12ii-complex(0,dQ12);
.... and so on ...
if U0-max(U1,U2) <= e
break;
end
end
Light
Light le 26 Mai 2013
Modifié(e) : Light le 26 Mai 2013
Thank you very much!! Almost done.
But it stopped at iteration 1 unless prove that U0-U2 <= e.
It must reach iteration 3
How can i give iteration number?
And can i model it in Simulink by whileloop block?
It is embarrassing asking so many thing Sorry :-)
Walter Roberson
Walter Roberson le 26 Mai 2013
No, the code will stop because it finds that U0-U2 <= e . Remember what I wrote earlier about you starting out U1 and U2 with the same value. Any algorithm that tries to iterate to find something between the two of them is going to stop because they are the same already.
I do not know what you mean about "How can I give iteration number" ?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by