What causes this variation?

조회 수: 1 (최근 30일)
Alex
Alex 2012년 2월 16일
편집: Anthony 2013년 10월 15일
Hey all, I've got to nearly identical programs, and one of them works while the other doesn't. When I run:
data = csvread('data_n.csv');
assert (mod(size(data, 1), 1) == 0, ...
'Input data must have an integer multiple of 1 rows');
assert (size(data, 2) == 6, ...
'Input data must have exactly six columns.');
y = data(:,1);
n0 = data(:,2);
R = data(:,3);
k = data(:,4);
n2 = data(:,5);
k2 = data(:,6);
R=(((n0-(n-1i.*k))./(n0+(n-1i.*k)))+(((n-1i.*k)-(n2-1i.*k2))./((n-1i.*k)+(n2-1i.*k2))).*(exp(-2.*1i.*(2.*pi./y).*(n-1i.*k).*120))./(1+((n0-(n-1i.*k))./(n0+(n-1i.*k))).*(((n-1i.*k)-(n2-1i.*k2))./((n-1i.*k)+(n2-1i.*k2))).*(exp(-2.*1i.*(2.*pi./y).*(n-1i.*k).*120))));
fid=fopen('results_n.csv','w');
fprintf(fid, '%5.5f\n', n);
fclose(fid);
I get an output in my .csv file of all the n values for the data set, and they're all fairly accurate. When I run this code, however:
data = csvread('data_d.csv');
assert (mod(size(data, 1), 1) == 0, ...
'Input data must have an integer multiple of 1 rows');
assert (size(data, 2) == 6, ...
'Input data must have exactly six columns.');
y = data(:,1);
n0 = data(:,2);
R = data(:,3);
n = data(:,4);
n2 = data(:,5);
k2 = data(:,6);
k = 0;
R=(((n0-(n-1i.*k))./(n0+(n-1i.*k)))+(((n-1i.*k)-(n2-1i.*k2))./((n-1i.*k)+(n2-1i.*k2))).*(exp(-2.*1i.*(2.*pi./y).*(n-1i.*k).*d))./(1+((n0-(n-1i.*k))./(n0+(n-1i.*k))).*(((n-1i.*k)-(n2-1i.*k2))./((n-1i.*k)+(n2-1i.*k2))).*(exp(-2.*1i.*(2.*pi./y).*(n-1i.*k).*d))));
fid=fopen('results_d.csv','w');
fprintf(fid, '%5.5f\d', d);
fclose(fid);
I get this error:
>> run d_solve
??? Error using ==> fprintf
Function is not defined for 'sym' inputs.
Error in ==> d_solve at 29
fprintf(fid, '%5.5f\d', d);
Error in ==> run at 74
evalin('caller',[script ';']);
As far as I can tell, there isn't any relevant difference between how the variables being output are treated, but I guess that's why I'm asking, right? Any ideas?
Thanks.
  댓글 수: 1
Alex
Alex 2012년 2월 16일
Oh, as a note, in the fprintf of the second program, I altered the %5.5f\d to %5.5f\n.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 16일
Neither version of the program can function. In the first version, you have no definition of "n" before you use it in "R" and try to print its value in the fprintf(). In the second version, you have no definition of "d" befire you use it in "R" and try to print its value in the fprintf().
In both versions you assign R = data(:,3) but then you overwrite R with a complicated expression.
The values being written, n and d, have no relevance to anything before that point in the programs.
The behavior you are seeing would be consistent with you having initialized "n" to a numeric value before running the first version, but having used
syms d
before the second version.
  댓글 수: 1
Alex
Alex 2012년 2월 16일
That makes sense. How would you recommend I go about correcting the error?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by