Why is this variable "undefined"?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey all, here's my code:
%This forgram will take 4 solutions of n1 and solve them.
y = 250;
n0 = 1;
n2 = 1.69;
k2 = 3.67;
R = .75;
for n1 = [2:.01:1.5];
n_new_1 = (pi*d1*n1)/atan((n1*(- R^4*k2^4*n0^2 - 2*R^4*k2^2*n0^3*n2 - 2*R^4*k2^2*n0^2*n1^2 - 2*R^4*k2^2*n0^2*n2^2 - 2*R^4*k2^2*n0*n1^2*n2 - R^4*n0^4*n2^2 - 2*R^4*n0^3*n1^2*n2 - 2*R^4*n0^3*n2^3 - R^4*n0^2*n1^4 - 4*R^4*n0^2*n1^2*n2^2 - R^4*n0^2*n2^4 - 2*R^4*n0*n1^4*n2 - 2*R^4*n0*n1^2*n2^3 - R^4*n1^4*n2^2 + 2*R^2*k2^4*n0^2 + 4*R^2*k2^2*n0^2*n1^2 + 4*R^2*k2^2*n0^2*n2^2 + 2*R^2*n0^4*n2^2 + 2*R^2*n0^2*n1^4 - 8*R^2*n0^2*n1^2*n2^2 + 2*R^2*n0^2*n2^4 + 2*R^2*n1^4*n2^2 - k2^4*n0^2 + 2*k2^2*n0^3*n2 - 2*k2^2*n0^2*n1^2 - 2*k2^2*n0^2*n2^2 + 2*k2^2*n0*n1^2*n2 - n0^4*n2^2 + 2*n0^3*n1^2*n2 + 2*n0^3*n2^3 - n0^2*n1^4 - 4*n0^2*n1^2*n2^2 - n0^2*n2^4 + 2*n0*n1^4*n2 + 2*n0*n1^2*n2^3 - n1^4*n2^2)^(1/2) - k2*n1^3 + k2*n0^2*n1 + R^2*k2*n1^3 - R^2*k2*n0^2*n1)/(R^2*k2^2*n0^2 + R^2*n0^2*n2^2 + 2*R^2*n0*n1^2*n2 + R^2*n1^4 - k2^2*n0^2 - n0^2*n2^2 + 2*n0*n1^2*n2 - n1^4));
n_new_2 = -(pi*d1*n1)/atan((n1*(- R^4*k2^4*n0^2 - 2*R^4*k2^2*n0^3*n2 -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^2*n0^2 + n0^2*n2^2 + 2*n0*n1^2*n2 + n1^4));
nd
fid=fopen('results_n_solve.csv','a');
fprintf(fid,'%5.5, %5.5, %5.5, %5.5f\n',n_new_1,n_new_2,n_new_3,n_new_4);
fclose(fid);
When I try to run this code, I get an error that reads:
Error in ==> n_iterations at 22
fprintf(fid,'%5.5, %5.5, %5.5,
%5.5f\n',n_new_1,n_new_2,n_new_3,n_new_4);
Error in ==> run at 74
evalin('caller',[script ';']);
Why am I getting this error? Isn't n_new_1 defined as the solutions above?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 21 Feb. 2012
Your loop
for n1 = [2:.01:1.5];
has its end point (1.5) greater than its start point (2), so it is never executed.
If you want a loop to count down, you must use a negative increment (e.g., -0.01) rather than a positive one.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!