Matrix Output displays Incorrectly
Mostrar comentarios más antiguos
Alright folks, so I'm brand new here and to matlab so this may be a dumb question. Anyway, I've created the following code to solve a nonlinear equation w/ the secant method. and I can't get mymatrix "ansdat" to display properly:
function f2 = BBSecantSolve()
%initial guesses vm is v_i-1 and v is v_i
vm = 0.05;
v = 0.02;
iter = 0;
disp(' itteration v(i-1) v(i) v(i+1) err f(v) f(vm)');
ansdat=[];
%for loop used to limit iterations to <= 100,000
for i=1:100000
iter = iter + 1; %counter for output
%eval f(vi) and f(vi-1) only once each loop
fv = BB(v);
fvm = BB(vm);
vp = v - fv *(v-vm)/(fv-fvm);% find vp according to secant formula
err = abs((vp - v)/vp);%find absolute standard error
ansdat=[ansdat;iter vm v vp err fv fvm];%add current values to output matrix
if err < 1e-4 %exit loop when the standard error is less than 0.0001
format short;%numerical output format
disp(ansdat);%show the matrix with all iteration values
f2 = vp;%give final answer
return
end
%set next loop values for vm and v
vm = v;
v = vp;
end
%error message in the case loop reaches 100,000 iterations
disp('maximum iterations reached, try a different initial guess');
function f = BB(vol)
%Beattie-Bridgman constants specific to CO2
A0 = 507.2836;
a = 0.07132;
B0 = 0.10476;
b = 0.07235;
c = 660000;
%Other System Variables
Ru = 8.314; %kPa * m^3 / (kmol * K)
P = 10132.5; %kPa
T = 300; %K
m = 0.001; %kmol
f = Ru*T/(vol^2)*(1-c/(vol*T^3))*(vol+B0*(1-b/vol))-(A0*(1-a/vol))/vol^2-P;
end
end
using the debugging tools I get the value of 'ansdat' right before my disp(ansdat) line to be (fyi this looks like the values I expect with the counter in the left most column increasing by one each iteration):
1.000 0.050 0.020 0.051 0.605 3596341.147 78007.417
2.000 0.020 0.051 0.051 0.012 73362.643 3596341.147
3.000 0.051 0.051 0.062 0.171 69193.920 73362.643
4.000 0.051 0.062 0.069 0.101 27424.121 69193.920
5.000 0.062 0.069 0.078 0.112 15248.698 27424.121
6.000 0.069 0.078 0.085 0.089 7092.634 15248.698
7.000 0.078 0.085 0.092 0.070 3246.279 7092.634
8.000 0.085 0.092 0.096 0.042 1254.512 3246.279
9.000 0.092 0.096 0.097 0.017 368.021 1254.512
10.000 0.096 0.097 0.098 0.003 61.533 368.021
11.000 0.097 0.098 0.098 0.000 3.753 61.533
12.000 0.098 0.098 0.098 0.000 0.041 3.753
However, the output that prints is:
0.0000 0.0000 0.0000 0.0000 0.0000 3.5963 0.0780
0.0000 0.0000 0.0000 0.0000 0.0000 0.0734 3.5963
0.0000 0.0000 0.0000 0.0000 0.0000 0.0692 0.0734
0.0000 0.0000 0.0000 0.0000 0.0000 0.0274 0.0692
0.0000 0.0000 0.0000 0.0000 0.0000 0.0152 0.0274
0.0000 0.0000 0.0000 0.0000 0.0000 0.0071 0.0152
0.0000 0.0000 0.0000 0.0000 0.0000 0.0032 0.0071
0.0000 0.0000 0.0000 0.0000 0.0000 0.0013 0.0032
0.0000 0.0000 0.0000 0.0000 0.0000 0.0004 0.0013
0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0004
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Can anyone tell me what in the world is going on!?
Thanks in advance
Respuesta aceptada
Más respuestas (1)
dpb
el 18 de Sept. de 2013
...
ansdat=[ansdat;iter vm v vp err fv fvm];
if err < 1e-4 %exit error is less than 0.0001
format short; %numerical output format
disp(ansdat); %show the matrix with all iteration values
...
You've placed all the data in the single array so Matlab by default will scale disparate ranges of exponents; if you'll note on the display there will be a header line that say something like
1.0e-06 *
above the data which when multiplied by the values gives the correct numbers. Of course, there's the problem of not enough significant digits to show the same precision across a wide range of exponents in the same array.
To make the data fit, you need to use fprintf() with an appropriate format string for the values you have.
fmt=[repmat('%14.3f',1,7) '\n'];
fprintf(fmt,ansdat')
NB the ' transpose operator to output the internally column-major storage order by row.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!