Question: ----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- function trapezoidArea(equation, xMin, xMax, numTrapezoids) local area = 0 local width = (xMax - xMin) / numTrapezoids for i

----------------------------------------------------------------------------------------- -- -- main.lua -- -----------------------------------------------------------------------------------------

function trapezoidArea(equation, xMin, xMax, numTrapezoids) local area = 0 local width = (xMax - xMin) / numTrapezoids for i = 0, numTrapezoids - 1 do local x1 = xMin + i * width local x2 = x1 + width local y1 = equation(x1) local y2 = equation(x2) area = area + 0.5 * (y1 + y2) * width end return area end

-- Define the relative error function function relativeError(calculatedArea, actualArea) local error = math.abs(calculatedArea - actualArea) / actualArea return error end

-- Create a user interface that allows the user to input the equation, x-limits, and number of trapezoids local input = native.newTextField(display.contentCenterX, display.contentCenterY - 100, 300, 50) local submit = display.newText("Submit", display.contentCenterX, display.contentCenterY) submit:addEventListener("tap", function() -- Parse the user input to extract the equation, x-limits, and number of trapezoids local equation, xMin, xMax, numTrapezoids = input.text:match("y = (.+), xMin = (.+), xMax = (.+), numTrapezoids = (.+)") -- Convert the input strings to numbers xMin, xMax, numTrapezoids = tonumber(xMin), tonumber(xMax), tonumber(numTrapezoids) -- Convert the equation string to a function equation = loadstring("return function(x) return " .. equation .. " end")() -- Calculate the area under the curve local area = trapezoidArea(equation, xMin, xMax, numTrapezoids) -- Display the area under the curve local areaText = display.newText("Area: " .. area, display.contentCenterX, display.contentCenterY + 50) -- Plot the curve and the area under the curve local curve = display.newLine(xMin, equation(xMin), xMax, equation(xMax)) local areaRect = display.newRect(xMin, 0, xMax - xMin, area) -- Lookup the actual area for the given equation and x-limits local actualArea for i, test in ipairs(unitTests) do if test.equation == equation and test.xMin == xMin and test.xMax == xMax then actualArea = test.actualArea break end end -- Calculate and display the relative error local relativeError = relativeError(area, actualArea) local errorText = display.newText("Relative Error: " .. relativeError, display.contentCenterX, display.contentCenterY + 100) end)

-- Define some unit tests local unitTests = { {equation = "x^2", xMin = 1, xMax = 2, numTrapezoids = 1, actualArea = 2.3333}, {equation = "x^2 - 4", xMin = -2, xMax = 2, numTrapezoids = 2, actualArea = 10.6667}, {equation = "-x^3 + 6x^2 - x + 17", xMin = 2, xMax = 4, numTrapezoids = 3, actualArea = 80}, {equation = "math.sqrt(2x^3 + 2x^2 - 5*x + 3)", xMin = -2.5, xMax = 7, numTrapezoids = 4, actualArea = 65.2502} }

-- Loop through the unit tests and run them for i, test in ipairs(unitTests) do local area = trapezoidArea(loadstring("return function(x) return " .. test.equation .. " end")(), test.xMin, test.xMax, test.numTrapezoids) local relativeError = relativeError(area, test.actualArea) print("Test " .. i .. ": Area = " .. area .. ", Relative Error = " .. relativeError) end This code is not working in Solar2D and Lua. Please solve it and give out put screen shot.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!