There must be Error using fprintf Unable to convert \'sym\' value to \'double\'.
ID: 3710760 • Letter: T
Question
There must be Error using fprintf
Unable to convert 'sym' value to 'double'.
Error in Quiz2pt2 (line 65)
fprintf('There must be %4.2f moles of NH3 produced to raise the blood pH back to a normal
7.4. ',Z(1));
Why am I receiving this error??
Code:
%% Analyze:
% Assume: Open, Reactive, Dynamic
% Basis/Given:
Ka=5.6*10^-10; %M
Vblood= 5000; %mL
pHi=7.2;
pHf=7.4;
mAo=(60/100)*Vblood; %ug
MWnh4=18039000 %um/mol
nAo=mAo/MWnh4
nHo=10^(-pHi)*Vblood;
nHf=10^(-pHf)*Vblood;
% HA: nHAq=nHAf-nHAo
% H: -nHc=nHf-nHo
% A: nAi-nAc=nAf
%% Calculate:
syms nHAo Kap nHo nAo
E1='Kap=Vblood*Ka';
E1a='Kap =((nHo*nAo)/nHAo)';
E1b='nAo=(60/100)*Vblood';
E1c='nHo=10^(-pHo)*Vblood';
X=solve(E1,E1a,E1b,E1c,nHAo,Kap,nHo,nAo);
nHAo=X.nHAo;
%n.Hi-n.Hc=n.Hf-n.Ho
%-n.Ac=n.Af-n.A0
%n.HAg=n.HAf-n.HAo
%Kap=(n.hf*n.Af)/n.HAf
%n.Hc=n.Ac
%n.Ac=n.HAg
Kap=Vblood*Ka;
nAo=(60/100)*Vblood;
nHo=10^(-pHi)*Vblood;
M=[1,-1,0,0,0,0;
0,0,-1,-1,0,0;
0,0,0,0,1,-1;
0,0,0,-nHf,0,Kap;
0,1,-1,0,0,0;
0,0,1,0,-1,0];
b=[nHf-nHo;
-nAo;
-nHAo;
0;
0;
0];
Z=M;
Z(1)
%% Finalize:
fprintf('There must be %4.2f moles of NH3 produced to raise the blood pH back to a normal 7.4. ',Z(1));
Explanation / Answer
Following reasons for the error and it's solution
1) Your input variable contains lots of symbolic variables and -ve exponent values.
To solve this, you can use VPA function(variable precision arithmetic)
for example-
R=[a/c a^-3]
M=[cos(c) sin(c/9); exp(c*a) (a*c)/log(c)]
Z=(vpa(R)/vpa(M))
2) To solve a high-degree polynomial expression using "solve".
In your case-
nHo=solve(10^(-pHi)*Vblood);
nHf=solve(10^(-pHf)*Vblood);
After that you should use VPA function to print final result.
3) In your program you are trying to convert high-degree polynomial expression into doule type array. And in matlab you can not convert symbolic expression into double tyoe array.
Solution to this proble is- Either you convert all the values of variables into non-symbolic expression using VPA function first and, then calculate value of Z in your final solution. or first use "eval" function to calculate the expression for each variables and then use vpa function. For example- R=[eval(a/c a^-3)]
M=[cos(c) sin(c/9); exp(c*a) (a*c)/log(c)]
Z=(vpa(R)/vpa(M))
4) If all above solution is not working, you can try to print value of Z by directly converting into double value (You will get result, but in this method you will loose some decimal data)
for example-
R=[a/c a^-3]
M=[cos(c) sin(c/9); exp(c*a) (a*c)/log(c)]
Z=double(vpa(R)/vpa(M))
fprintf('%4.2f',Z);
Please let me know if you are still facing this getting this error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.