0

I want to pass an equation as a parameter in a program when called so I can evaluate it. My goal is to allow the equation from parameter to have a value plugged in to solve, but the value plugged is based on part of program. I also want equation to be adjustable, not hard coded by passing as parameter on program call. How can I do this?

Edit: I have tried passing it as a string in an parameter to be called.

Function("equation")

And then using define to make it a sub function in my program, then call it that way with a var passed. I can't get that to work however, I get an error: "variable undefined."

I think the issue is linking var passed to sub function to var in equation given. I also tried just passing equation, no quotes, but got similar error.

  • What have you tried so far? It's a little hard for readers to know what you're asking for unless you "set the scene" for them. I know Lua pretty well, for example, but have no experience with TI-Nspire. Still, I *might* be able to help, but it's impossible to know without more information. (See [this page](http://stackoverflow.com/help/how-to-ask) for advice on to write great questions.) – evadeflow Dec 06 '16 at 19:53
  • @evadeflow I edited it a bit to add some more info on what I've tried. I think the issue might moreso be with how the calculator treats input. Have you tried to store an equation in a variable, then plug a value into the stored equation? – user7258936 Dec 06 '16 at 20:14
  • No, sorry, I only just learned (from [this page](https://education.ti.com/en/us/solutions/lua_scripting/landing)) that TI-Nspire is a graphing calculator that supports Lua. It looks cool! But I don't have any insight into how to make it do what you want. It *sounds* like you want to pass a primitive type into a function by reference, but (AFAIK) that only works with tables and functions. You might try packing your function parameters into a Lua table to force copy by reference. See [this thread](http://lua-users.org/lists/lua-l/2004-06/msg00257.html) for possible suggestions. – evadeflow Dec 06 '16 at 20:40
  • @evadeflow thanks, I'll take a look. My end goal is to write a very primitive equation solver to use trapezoid rules to mimic integral (AP calculus AB curriculum involved trapezoid rules) and I wanted the challenge to write a program to solve it. Unfortunately, more of my knowledge is in python and java, not lua. When I tried to search it in google, I had no luck finding other questions like it with answers. Thanks for your help. I'll try that. – user7258936 Dec 06 '16 at 20:44
  • Sure. [This post](http://lua-users.org/lists/lua-l/2004-06/msg00293.html) from the aforementioned thread seems close to what you want. The poster is asking how a function declared as `function Optimize(f, v, min, max)` can solve for `v` in a way that optimizes the passed-in function `f`, and he wants to set `v` in `Optimize()` and have the modifications show up in the caller's scope. If that's what you're after, don't give up! You'll have to do some slight restructuring to get around Lua's inability to pass primitive values by reference, but packing everything into a table ought to work. – evadeflow Dec 06 '16 at 21:05
  • @evadeflow I'll take a look at that too. Thanks for that! – user7258936 Dec 06 '16 at 21:08

2 Answers2

0

Here is one way to do it. The calculations were done in a Notes.

enter image description here

soegaard
  • 30,661
  • 4
  • 57
  • 106
0

Maybe something like this could work for you (?):

Define LibPub foo(x,fun)=
Func
Local f
expr("f(x):="&string(fun))
Return f(x)
EndFunc

Then you can call foo with the function/equation you want, e.g. foo(5, x-7)

ThePrancingPony
  • 301
  • 2
  • 5