MATLAB Answers

Finishing a calculation

2 views (last 30 days)
Alex
Alex on 23 Feb 2012
Hey all, I've been analyzing lots of data recently, and thanks to everyone here, I've made great progress in learning Matlab and getting it to work. I have a new issue, but it should be resolved easily.
I've been running a loop to solve for a large amount of data imported from a csv file. I've been having success up until now. Here's the code:
data = csvread('data_d1_multi.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.');
syms d1;
nsys = size(data, 1);
for k = 1 : nsys,
F = solve_d1_multi_eq(data((k-1) + (1:1), 1:end));
d(k, :) = solve(F);
end
fid=fopen('d1_multi_values.csv','w');
fprintf(fid,'%5.5f\n',d);
fclose(fid);
The function file in the above code brings in the data from the csv file and creates the function F, which is then solved, and then the loop reoccurs. It also defines d1 as a symbol. When I get to the fprintf step, however, Matlab won't write. Here is the error:
??? Error using ==> fprintf
Function is not defined for 'sym' inputs.
When I enter d into the command window, I get a list of "values," and these values are mathematical forms that, when I put them individually into the command window, solve into actual numbers. They look like so:
-(1255*log((117428176152016452228945106049267^(1/2)*(14665804317275172766575378895460*i - 2528628208763574832888728359811)^(1/2))/89293179851920557132557466203166 + 30173428047464645764307857899481/44646589925960278566278733101583 - (50832467171298678039874147590785*i)/89293179851920557132557466203166)*i)/(32*pi)
So, my questions:
1) How do I help Matlab output the values?
2) Why doesn't Matlab finish these calculations?
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 23 Feb 2012
As far as MATLAB is concerned, it has finished the calculations. It has given you exact analytic solutions, rather than mere 16 digit floating point approximations.
I notice that your outputs are complex valued. You need to take extra steps to output complex values properly.
dd = double(d);
fprintf(fid,'%+5.5f %+5.5fi\n', [real(dd(:)), imag(dd(:))].' );
Here the "i" after the "%+5.5f" is just a literal character to indicate the imaginary component.
In the example above it appears that the imaginary component vanishes in practice, which is not at all obvious from casual examination. If that is the situation for all values, then you could use
fprintf(fid,'%+5.5f\n', real(dd(:)) );
but unless you can prove that the imaginaries will vanish, outputting them would be safer.
  1 Comment
Alex
Alex on 23 Feb 2012
Thank you very much! I found this to be very informative, and it cleared things right up.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!