What causes this variation?

1 view (last 30 days)
Alex
Alex on 16 Feb 2012
Edited: Anthony on 15 Oct 2013
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 Comment
Alex
Alex on 16 Feb 2012
Oh, as a note, in the fprintf of the second program, I altered the %5.5f\d to %5.5f\n.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Feb 2012
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 Comment
Alex
Alex on 16 Feb 2012
That makes sense. How would you recommend I go about correcting the error?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!