Equations, units and things.. (plus42)

General discussion about calculators, SwissMicros or otherwise
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Equations, units and things.. (plus42)

Post by dm319 »

Sorry for prolific posting today...

Got a few questions around free42/plus42 in general, if anyone can suggest answers and best practice!

Lets say I want to calculate EV range using this formula:

\(efficiency = \frac{capacity * charge}{distance}\)

1. How would I manage having two values for 'efficiency'. I.e. and upper and lower bound? Can that be done in an equation, or is it best to use a program?

I pushed on anyway and wrote it out as an equation but then ran into difficulties with units.

2. Do variables and units need to be enclosed in brackets?

This doesn't work:

A=B_"km"/C_"h"

But this does:

A=B_"km"/(C_"h")

Can I define units for 'A'? When I do it seems to come up with an invalid type error also.

3. Is there a way to assign solving an equation directly to a custom button?

I.e. can I skip the equation list view, and go directly to solving an equation?

I feel like this relates to "EQNSLV" function, but I can't quite figure out how.

4. Is there a way to coerce units as answers, that may not relate to the units used elsewhere in the equation?

I.e. I'm thinking of the awful units of EV efficiency that a car might read out - which includes kWh/100km (audi), kW/km (tesla) or m/kW (nissan). And I don't really want to be inputting '100km', but I might want kWh/100km.

Thank you,
D
SN:09075
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Ok I've figured some things out, and have created this equation to help with calculating the relationship between distance, efficiency and km. I created the franken-unit "hkm" (hectokilometers, actually quite pleased with myself here) by STO 100_km into "hkm". It was the round brackets that made it work this time...

EFF=(67.4_"kWh"*CHRG*0.01)/((DIST_"km")+(0_"hkm"))

Where EFF is efficiency in kWh/100km or kWh/hkm, CHRG is charge % of battery and DIST is distance in km.

This seems to work in that it fulfils me inputting km, but getting the bizarre efficiency unit back.

However, if I give it EFF and CHRG, DIST does not come back as km. I don't know why this is.
SN:09075
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

EFF_"kWh/hkm"=(67.4_"kWh"*CHRG*0.01)/((DIST_"km")+(0_"hkm"))


Ok, this doesn't throw any errors, but also doesn't return any units. Which makes me think that it only returns units if they aren't specified for the variable you are calculating? Hmmm
SN:09075
User avatar
Walter
Posts: 3070
Joined: Tue May 02, 2017 11:13 am
Location: On a mission close to DRS, Germany

Re: Equations, units and things.. (plus42)

Post by Walter »

Sorry, the system works differently:
There is no unit 'hkm'! You may express your efficiency results in Wh/hm if you like. And distances in Mm though that's quite unusual.

Looks like your CHRG is unitless, given in %. So why do you want to load your calculation with all that unit gymnastics? You throw kWh and hm in (or Wh and m) and get them out again without any witchcraft or sorcery. So where's the benefit of this whole effort?
WP43 SN00000, 34S, and 31S for obvious reasons; HP-35, 45, ..., 35S, 15CE, DM16L S/N# 00093, DM42β SN:00041
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Walter wrote:
Mon Aug 15, 2022 11:52 pm
There is no unit 'hkm'!
Yes, made it up!
Walter wrote:
Mon Aug 15, 2022 11:52 pm
Wh/hm
That seems far more sensible.
Walter wrote:
Mon Aug 15, 2022 11:52 pm
So where's the benefit of this whole effort?
Agreed, not much! It would be nice to see your result with a corresponding unit, and that could work as a double check to ensure you are calculating the correct value, pressing the right buttons.
SN:09075
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Equations, units and things.. (plus42)

Post by Thomas Okken »

dm319 wrote:
Mon Aug 15, 2022 7:27 pm
2. Do variables and units need to be enclosed in brackets?

This doesn't work:

A=B_"km"/C_"h"

But this does:

A=B_"km"/(C_"h")
The underscore is a multiplicative operator, so it has the same precedence as * and /, that's why the parentheses are needed in your example. I should mention that in the documentation.

(I'll look into your other questions later, just thought I'd answer this one quickly while I have a moment.)
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Equations, units and things.. (plus42)

Post by Thomas Okken »

Regarding units in the solver: if the equation is solved by the direct solver, the result will simply have whatever units come out when the symbolic inverse of the equation is evaluated; when the numerical solver is used, the units of the result will match the units you provided in the starting guess(es). Note that you do have to make sure the starting guesses have correct units in that case.
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Equations, units and things.. (plus42)

Post by Thomas Okken »

Thomas Okken wrote:
Tue Aug 16, 2022 6:07 pm
Regarding units in the solver: if the equation is solved by the direct solver, the result will simply have whatever units come out when the symbolic inverse of the equation is evaluated
That's actually not very helpful behavior, now that I think about it... I'll change it to convert the result to the unit of the previously existing value of the result variable, if it exists and if it is compatible. That would be more in line with the behavior of the numerical solver and the behavior of VARMENU.
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Thomas Okken wrote:
Tue Aug 16, 2022 6:07 pm
symbolic inverse of the equation is evaluated
Thanks Thomas, though you have highlighted to me my lack of understanding of this aspect of calculators! I did some googling and I can see that these calculators seem to solve the equations much like I was taught to do in school. I'm very impressed! Did you make your own implementation to code that into plus42? I'm going to have to look into how the solver works in more detail, so I imagine I'll need to look over the 19bii manual... If you have a suggestion for how I could coerce the result to give me a unit I'd be very curious! Also, if I wanted a range, would you recommend using matrices for that? Or maybe these new lists? Thanks!
SN:09075
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Equations, units and things.. (plus42)

Post by Thomas Okken »

dm319 wrote:
Wed Aug 17, 2022 12:51 am
I did some googling and I can see that these calculators seem to solve the equations much like I was taught to do in school. I'm very impressed! Did you make your own implementation to code that into plus42?
Yes, I wrote my own direct solver implementation, with the same functionality as the direct solver in the HP-17B, 27S, etc. It's actually a rather straightforward algorithm, nothing like the symbolic solvers in full-blown CAS systems like you have in Mathematica or in later HP calculators.
dm319 wrote:
Wed Aug 17, 2022 12:51 am
If you have a suggestion for how I could coerce the result to give me a unit I'd be very curious! Also, if I wanted a range, would you recommend using matrices for that? Or maybe these new lists? Thanks!
For converting one number to use the same unit as on another number, simply use the CONVERT function. This converts the number in the Y register to the unit of the number in the X register, while ignoring the numeric part of the X register.

Regarding ranges, that's not something that's supported in Plus42. Writing your own implementation in user code wouldn't be an easy project, I imagine. Lists would be the best fit since they can hold values of any type, while matrices are limited to only real numbers and strings (for real matrices) or only complex numbers (for complex matrices). You could store numbers with units in real matrices by storing the numeric part separately from the unit, and storing the unit as a string... But both methods have the problem that you can't see at a glance what's inside those objects, since all that's shown in the display is [ MxN Matrix ] or { N-Elem List }. (I'm planning to make it show the contents of such objects in a more RPL-like manner in a future update, but it may be a few months before I have time to work on that.)
Post Reply