Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

first degree equation in LUA (Off?)
Started by pabloapdz Nov 15 2018 10:37 AM

7 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 127 posts
  • Corona SDK

Good afternoon,

 

I know it may seem like a stupid question, something very simple, but I really looked for something similar and I did not think how to do it, I'm not sure if this is off topic, but it's because it's a LUA question that I'll use in Corona, so I'm kind of confused about that.

 

I am doing a job in my college and I have decided to use the programming to present it, but the work involves first-degree equations, (if you have someone who studies engineering here, I am making a formula for calculating the vertical and horizontal forces of a bar ), but I came across the following problem ...

 

How do I make first-degree equations in LUA?

For example ..

2x + 28 = 0 ...

x = 14

 

Obviously it's not that simple, because otherwise I'd have to do it manually, but the problem is that there are many possibilities, and I can not call "x" from 0 otherwise it will multiply by 0 in the future and my equation will give wrong.

 

Does anyone know how I can do this?



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

You're trying to write Lua code to solve for X.

 

Try a google search and you may be surprised at what you find.

 

Google: solve for x lua



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,259 posts
  • Enterprise

you're in college, studying engineering, and doing programming, yet stumped by fifth grade math???  (sounds fishy)

 

first note that the proper solution would be x = -14

how:  rewrite left side with variables: ax+b=0, then solve:  x=-b/a



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

Try this: 

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/solveForX.zip



[TOPIC: post.html]
#5

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,259 posts
  • Enterprise

wrong approach (and may not even work for given equation) when exact solution is so trivial:

-- solves first-order equations of the form ax+b=0
local function solve(a,b) return -b/a end
print("2x+28=0, x=", solve(2,28))

which feels like i've just done someone's middle school programming homework for them :(


  • roaminggamer likes this

[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

I updated the example.  Download it again

 

 

Then, try the smart solver:

local solver = require "solver"

-- Cool
local function my_func(x)
   return 200/(x+x^2+x^3+x^4+x^5) - 0.00001001
end
print( solver.solve( my_func )  )


-- Cooler
local x = solver.solve( "200/(x+x^2+x^3+x^4+x^5) - 0.00001001", {debugEn = true} )
local x = solver.smartSolve( "2 * x + 28", { debugEn = false } )

print("Solution : " .. tostring(x))




[TOPIC: post.html]
#7

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 127 posts
  • Corona SDK

First, @roaminggamer, thank you very much for your help, I could not come here to answer you, because I'm at the end of the semester, every day next week has proof or work to present, it's a gigantic run, but I was already going to come here.

It turned out that I found its result there too great for my problem, it would work harder than if I broke my mind to do it in a functional way, then I sat down and managed to develop this :( Sorry for the variables being in Portuguese, but translate now would do a great job and I have to deliver this program tomorrow at 7pm)

if ValorDaValorDaCargaNaBarra ~= nil then

if AnguloDaForca == 90 then

  SomaHorizontal = 0

  SomaVertical = ValorDaValorDaCargaNaBarra

  if DistanciaDaCarga > DistanciaApoioFixoNaBarra then

   Soma = -ValorDaValorDaCargaNaBarra * (DistanciaDaCarga - DistanciaApoioFixoNaBarra)

  else

   Soma = ValorDaValorDaCargaNaBarra * (DistanciaApoioFixoNaBarra - DistanciaDaCarga)

  end

else

  -- Somatorio forças horizontais

  SomaHorizontal = ValorDaValorDaCargaNaBarra * math.cos(math.rad(AnguloDaForca))

  print("Fx = "..SomaHorizontal .. "Utilizando angulo de " .. AnguloDaForca)

  if ApoioMovelNaBarra == nil then

   Soma = ValorDaValorDaCargaNaBarra * math.sin(math.rad(AnguloDaForca))

   SomaVertical = Soma

   print("Fy = "..Soma .. "Utilizando angulo de " .. AnguloDaForca)

  else

   -- Somatorio Momento

   if DistanciaDaCarga > DistanciaApoioFixoNaBarra then

    Soma = -ValorDaValorDaCargaNaBarra * math.sin(math.rad(AnguloDaForca)) * (DistanciaDaCarga - DistanciaApoioFixoNaBarra)

    --print("Soma = -"..ValorDaValorDaCargaNaBarra .. " * " .. math.sin(math.rad(AnguloDaForca)) .. " * (" .. DistanciaDaCarga .. " - " .. DistanciaApoioFixoNaBarra .. ")" )

    SomaVertical = ValorDaValorDaCargaNaBarra * math.sin(math.rad(AnguloDaForca))

    print("Fy = "..Soma .. "Utilizando angulo de " .. AnguloDaForca)

   else

    Soma = ValorDaValorDaCargaNaBarra * math.sin(math.rad(AnguloDaForca)) * (DistanciaApoioFixoNaBarra - DistanciaDaCarga)

    print("Fy = "..Soma .. "Utilizando angulo de " .. AnguloDaForca)

   end

  end

end

end

I left the code here if someone in the future needs something like this :)

Thank you very much !

 

 

@davebollinger

 

Yes I know how to solve integral, derived, bla bla bla bla and all that chatura there, and me problem was not the resolution of the question (I would never do this program if it were depending on me, believe me, with a paper and a pen and a calculator I can solve it faster than someone entering the information, but this is worth 20 points, and 20 points at the end of the semester, right!

 

 

I do not go around thinking that anyone is a liar, I'm an engineering student, and probably what I study you've never heard of, (things like stratigraphy of rocks, lithodemic unit, igneous rock, metamorphic, particle grain, dungeon, etc), and yes, I still program but direct I come here to the forum ask for help, sometimes for things that may seem very simple for you being a bug of seven heads for me, after all, I did not do programming and also did not college I did some course, I am always learning every day that passes, one day who knows you still hear of me talking about some famous project hahahah

 

Note: my problem was not solving 2x + 18 = 0, my problem was an equation with a lot more problems than this, I wanted 0 to be a variable that would solve itself, without me having to tell the program to go there on the 18th then divide by 2, I wanted everything more automatic, if any 5th graders make me a program like that, well, then I would have to agree that my IQ might look a bit like his.

 

Bye bro :)



[TOPIC: post.html]
#8

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 769 posts
  • Corona SDK

my problem was an equation with a lot more problems than this

Details, please.  :) I have some math libraries (mainly linear algebra, but also bits and pieces of other things) that I haven't been able to give much love lately, but I'm always interested in use cases.




[topic_controls]
[/topic_controls]