Why getting some strange fractions? The code is simple and correct,maybe.

why these fraction?
nuove_pp(reag+i) -> ..and.. -> nuove_pp ->
nuove_pp = 1×4
14.6794 9.7862 44.0381 39.1449
nuove_pp
reag
prod
cs
prodotti_numeratore=1;
for i=1:prod
prodotti_numeratore = prodotti_numeratore.*((nuove_pp(reag+i)+cs(reag+i)*x)^cs(reag+i)) ;
end
reagenti_denominatore=1;
for i=1:reag
reagenti_denominatore = reagenti_denominatore*((nuove_pp(i)-cs(i)*x)^cs(i)) ;
end
Q=prodotti_numeratore /reagenti_denominatore
equ= Q == K_P;
sol=double(solve(equ,x,'Real',true))

Respuestas (6)

Stephan
Stephan el 17 de Sept. de 2018
Editada: Stephan el 17 de Sept. de 2018
Hi,
if you calculate symbolic equation, the calculated values (like factors for example) in symbolic equations are represented exactly, which means that they look like this.
If you want to have double representation use:
double(a)
If you create function handles from your symbolic equation the values will be converted to double automatically.
A way for more precision than double is vpa like Star Strider suggested in his answer. If you want to solve symbolic equations numeric have a look at vpasolve.
Best regards
Stephan
Star Strider
Star Strider el 17 de Sept. de 2018
It looks as though you are using the Symbolic Math Toolbox.
To get the fractions as thier decimal equivalents, use the vpa (link) function.
thank you very much for your availability. The main question is: why is not simply inserted the value relative to pp_nuove (3)? Matlab generates a useless and inexplicably large fraction.
prodotti_numeratore=1;
for i=1:prod
prodotti_numeratore = prodotti_numeratore.*((nuove_pp(reag+i)+cs(reag+i)*x)^cs(reag+i)) ;
end

6 comentarios

Your nuove_pp is numeric (datatype double). You are combining it with the symbolic variable x. MATLAB cannot work on mixed double and symbolic expressions, so it converts the double to symbolic numbers. It does that by the equivalent of calling sym() on the numbers.
The default conversion for calling sym() on a double is the equivalent of using the two-input version of sym with the 'r' flag: e.g., sym(1.2345) is equivalent to calling sym(1.2345, 'r') . The 'r' flag tells sym() to examine the bit pattern to determine whether it is a special value such as pi, and if it is then it substitutes the symbolic form of the special value; if it is not one of those special values then the 'r' flag tells the symbolic engine to do something similar to a continued fraction to try to find a ratio of integers or a mix of integers and square roots of integers that approximate the floating point value. For example, sym(.33333333333333) will be converted to the rational fraction 1/3, and if you were to do A = sqrt(3)/2; sym(A) then the algorithm would be able to convert that bit pattern approximately 0.866025403784439 into the form sqrt(3)/2 /
If it bugs you that MATLAB does this instead of using floating point numbers, then explicitly convert the double to sym telling it to use floating point software representation:
prodotti_numeratore = prodotti_numeratore.*((sym(nuove_pp(reag+i),'f') + sym(cs(reag+i),'f') *x)^sym(cs(reag+i), 'f')) ;
Luca
Luca el 18 de Sept. de 2018
Editada: Luca el 18 de Sept. de 2018
With your solution, result is different (solve), why?
Luca's "Answer" moved here:
On paper I drew a blue box with the operations to be performed.
When you solve() a polynomial system with rational coefficients, solve returns all of the solutions (in an undefined order). When you solve a system with software floating point, solve may assume that you only want one solution, and which of the solutions it chooses is undefined and small changes in the values or starting point can have it pick a quite different solution.
Also, when software floating point is used, the meaning of of raising a value to a power is defined by
A^B = exp(B*log(A))
and for negative A that can end up creating a complex valued solution. For example (-1.0)^(1/3) is always a complex root of unity, never -1. Therefore when you use software floating point you can end up calculating something different.
When you use software floating point, approximations are made that can lead to quite different solutions. Consider
1/((10^40+1)-(10^40))
Algebraically this is 1/1 but
1/((10.^40+1)-(10.^40))
Will lose precision on the addition, causing this to evaluate as 1/0
If I knew exactly what input values you had used in your screen snapshots then I could trace to figure out the exact situation for you, but I was not able to figure out what mol inputs you used.
Luca's "Answer" moved here:
That's all.
nuove_pp = 1×4
14.6794 9.7862 44.0381 39.1449
reag=2;
prod=2;
cs=[1 1 1 1];
prodotti_numeratore=1;
for i=1:prod
prodotti_numeratore = prodotti_numeratore.*((nuove_pp(reag+i)+cs(reag+i)*x)^cs(reag+i)) ;
end
reagenti_denominatore=1;
for i=1:reag
reagenti_denominatore = reagenti_denominatore*((nuove_pp(i)-cs(i)*x)^cs(i)) ;
end
Q=prodotti_numeratore /reagenti_denominatore
equ= Q == K_P;

Iniciar sesión para comentar.

Luca
Luca el 18 de Sept. de 2018
Thank you, heartily. But why does the matlab result differ from the paper one? The implemented code is as simple as the exercise.

1 comentario

What input values should I be using? It looks like I should use 1 1 1 1 for the regents, but what should I enter for the mol values?

Iniciar sesión para comentar.

Luca
Luca el 20 de Sept. de 2018
Mol values?
do not need mol values, but only the partial pressures I have already indicated. Why do you ask me for the mol?
(Excuse me for my bad English)

1 comentario

Your code prompts four times for "Inserisci il coefficiente stechiometrico del reagente". This values get displayed directly on your sample output diagrams as "cs" just above "Q". It looks like the answers to each of those should be 1 .
Your code then prompts two times for "Assegnazione moli di reagente" and two times for "Assegnazione moli di prodotto". The English translation of that is "Assignment of moles of reagent", and "Assignment of moles of product" which appears to be questions about mole values that would be responded to with mol quantities, rather than a question about partial pressures. These values get displayed in your code as moli_reag and moli_prod a full page above the portion of the code that you have posted images of.
I need you to pick one particular set of "Inserisci il coefficiente stechiometrico del reagente" values and one particular set of "Assegnazione moli di reagente" and "Assegnazione moli di prodotto" values and tell me what they are. Do not assume that I can read them off of the diagrams: your diagrams are cut off.
This is not hard: just tell me which 8 input values are to be used.

Iniciar sesión para comentar.

Leave default values. The range could be from 0.1 to 4 with 0.1 step.
moli_reag(reag)=0;
for i=1:reag
prompt = 'Inserisci le moli di reagente:';
title = 'Assegnazione moli di reagente';
dims = [1 55];
defaultinput = {'0.40'};
moli_reagx= inputdlg(prompt,title,dims,defaultinput);
moli_reag(i)=str2double(moli_reagx);
end
moli_reag
PS thank you for taking the time.

1 comentario

Sorry, I do not seem to be able to assist you on this matter; the communications problems seem to be too large to overcome.

Iniciar sesión para comentar.

Productos

Versión

R2018a

Preguntada:

el 17 de Sept. de 2018

Comentada:

el 20 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by