Why is this variable "undefined"?

1 次查看(过去 30 天)
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 - 2*R^4*k2^2*n0^2*n1^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 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?

采纳的回答

Walter Roberson
Walter Roberson 2012-2-21
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 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by