Help with Homework

Hey guys, I am very new to coding (as in this is my first code). I don't fully understand that minutia that goes into coding, but I am trying to make my life a little easier when it comes to my Chemistry class. We are constantly having to solve for pH and concentrations of compounds. All of the compounds can be solved in terms of hydrogen concentration, H. I am trying to figure out why I get the error "target of assignment expands to non-language object" in my code. I think my problem has something to do for how I am defining H initially. I could also be using the completely wrong language for this sort of problem. Any feedback would be greatly appreciated. The professor told us today that the H value should equal 10^(-7.21)/

Ka1 <- 10^(-6.52)
Ka2 <- 10^(-10.56)
Kw <- 10^(-14.704)
Kso <- 10^(-8.09)
Kh <- 10^(-1.2)
Pco2 <- 10^(-3.5)
H.low <- 0
H.high <- Kw
H.int <- 0.001
for (H in seq(H.low,H.high,H.int)) {
OH <- Kw/H
denom <- (H^2)+(H*Ka1)+(Ka1*Ka2)
Alpha0 <- (H^2)/ denom
Alpha1 <- (H*Ka1) / denom
Alpha2 <- (Ka1*Ka2) / denom
H2CO3 <- Kh*50*Pco2
Ct <- H2CO3 / Alpha0
HCO3 <- Alpha1 * Ct
CO3 <- Alpha2 * Ct
Ca <- Kso / (Alpha2 * Ct)

OH + HCO3 + 2*CO3 - 2*Ca - H
}

solve.default (H,OH, H2CO3, HCO3,CO3,Ca,Ct)

Hi, and welcome!

Just for reference (you've got the basic ideas), see reproducible example, called a reprex and homework policy.

Assignment is an important part of the error message; it's either an explicit assignment such as

Ka1 <- 10^(-6.52)
Ka2 <- 10^(-10.56)
Kw <- 10^(-14.704)
Kso <- 10^(-8.09)
Kh <- 10^(-1.2)
Pco2 <- 10^(-3.5)
H.low <- 0
H.high <- Kw
H.int <- 0.001

(It's also possible to assign with the = operator, but good style encourages <- for creating objects and = for creating attributes in objects.)

or an implicit assignment in a function.

Your code has two functions -- the for loop and solve.default().

Let's look at for first. (BTW: take a look at purrr:map() which is tidier, a term you will grow to know and love here.)

What does for do? Well, for one thing, it creates an object OH. What is its value?

 OH
[1] Inf

Uh, oh. Even without getting into solve.default, it's easy to see that passing Inf as an argument to any function might be a problem.

So, where does Inf come from. Hint

> seq(H.low,H.high,H.int)
[1] 0
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.