Thought Process for Solving Algebra Equations? - javascript

I'm working on a graphing applications that basically graphs equations with on an HTML5 canvas. I had no problem graphing equations that were along the lines of y=3x^(2) etc. That was as easy as plugging in a given x value, substituting exponents for native functions and voila!
Ideally however, I'd like to graph equations for circles and other equations that don't necessarily start with y=.... This would require actually doing algebra, which, unfortunately is not so easy. My question is: what is the most logical way to solve a problem such as 3x+3y=15? Let's assume that I'm given an x and I'm solving for y. How would you go about creating a function that solves it?
Obviously, I could choose to be extremely inefficient and loop through y values until I find one that satisfies the equation, but let's try to avoid that.
I'm not asking for you to write the script for me, I'm just asking for the best/most efficient thought-process to get started.
Currently, this project is being written in Javascript.
Thanks!

One (approximate numerical) way is to take your equation re-write it as P(x) = 0 [in your case P(x) = 3(x^2) + 3(y^2) - 15] and then use a numerical technique such as Newton-Raphson to find the roots of P(x)
If you want to solve symbolically, then a Computer Algebra System (CAS) is required (non-trivial).

usually you would express the equation with one variable on one side of the equals sign and the other variable on the other.
If you want to rewrite equations form random user input, you will need some kind of parsing engine.
look here for a discussion

y=3x^(2) is not linear its quadatric, 3x+3y=15 is in fact linear.
It depends on how complex you want to go, it's not that challenging to write something to rearrange a linear equation like 3x+3y=15 into its standard linear form (y=5-x), but it gets harder fast and while there are probably server side libraries for it, i'm not sure about JS.

The proper name for what you are looking for: http://en.wikipedia.org/wiki/Computer_algebra_system

Related

Making a little angular resolution calculator in JS but it returns NaN

So, I'm in an astronomy class, and don't have a scientific calculator yet, so as a side project practice sometimes I'll translate a math question that would otherwise be tedious into a JS program so it can do physics math for me (like, last week i made an escape velocity function rather than punching in math individually to calculate escape velocity for multiple objects).
This new question basically states that Arcsine of theta = lightspeed/diameter of observation point (this is the equation the prof offered, though he said for the sake of simplicity we can use 1.00 rather than 1.22 in this measure as it is more precisely known).
I've designed a function to take diameter as a parameter and return the answer, but when I run it it just consoles NaN, I wonder if it's my logic or syntax that is faulting me or just poor math. I'm pretty rusty, but enjoy doing little side practices to probe into my comprehension of math equations and whatnot. If anyone felt like pointing out where I went wrong, it would help me diagnose what aspect of this process I am not retaining. I'm also like 4 drinks in and it's 1:30am tho lol this I guess is how I choose to spend my time. here goes:
let lightspeed = 3*Math.pow(10,8);
function Aungular (diameter){
let ans1 =lightspeed/diameter;
let ans =Math.asin(ans1);
console.log(ans);
}
Aungular(20); // returns NaN
What am I messing up?
Oh, also I have like no trig experience haha, so it's important that I diagnose early on if this is what's causing whatever I overlooked.
(I hope that this doesn't cross any boundaries -- no coding is relevant to the assignment - this is something I'm doing on my own time as a brain exercise to increase my depth of understanding for math problems and exercise practice for writing code more fluently).
Edit: I mixed placements for diameter and lightspeed, program runs if they are switched. Solved, I guess. It's always the simplest stuff that I miss haha

JavaScrpt math: solve simultaneous equation

I am new to JS, I need to solve this type of equation: https://www.desmos.com/calculator/zbqt1g27lb in JavaScript, is there any way to do this?
9x^2+4y^2-3x+4y=6
9x^2+4y^2+3x-3y=6
The coefficients may change, but the unknowns are always in that format.
Is there any function in JS.Math I can directly use?
I will be grateful for your help.
This is a maths problem, not a javascript problem. I would recommend solving it algebraically first (factorising to get one in terms of the other, and then using substitution) until you have an expression for x and y in terms of the coefficients. Then write javascript code to evaluate those expressions given the coefficients. You will probably need to evaluate four expressions, due to the ± nature of square roots.
There are various methods for computationally solving any equation, but they would be inefficient in this case, since the form of the equations are known.

Simple Linear Regression Prediction Algorithm in JavaScript

I am trying to do a simple profit prediction of an organization in the future based on the past profit in JavaScript. My dataset will be the date as x-axis and the profit as y-axis. I am new to data analytics and basically I have zero knowledge in it and I not sure which prediction algorithm will be the most suitable.
I have done some researches here and here and found out that I can actually use the Linear Regression Prediction algorithm. However, from those examples, I only saw that the prediction algorithm is simply plotting a straight line based on the data to find out the regression value and it does not predict any value for the future at all.
I wonder if the algorithm mentioned above is applicable for my case?
Thanks!
It depends a lot on the business and how much data you have. Does the past history follow a regular linear progression? If so then a linear model would make sense. Are there ups and downs? What explains those? Is it seasonal or some other cyclical? If so you need to take those into account. Are there specific periods with huge outliers that are very uncommon? Perhaps correcting (removing) those would yield better results.
There is no one-size-fits-all solution.
The prediction has nothing to do with JavaScript or HTML. It's just using a regression function. How to fit your function to data granted is a realm of regression analysis. You can check the least squares method to clarify your understanding.
Choosing of regression function is another issue. It's related to realm your data come from. You have to be aware of restrictions on your output so you can take a function that will fit the business logic (for example, if you have your data cyclic by a year or a day or whatever, you may wrap sin() or cos() function over another one).
There is one more method to predict. It's related to machine learning and based on artificial neural networks. If you're interested doing this with JS, I can suggest you use brain.js - the simplest library to deal with neural networks in JS.

Use of letters for doing matrix math in Javascript

I'm doing a course in Quantum Computation. In it, we represent possible actions, or operators, by matrices. I've been looking into creating a webpage for solving these maths problems.
It is also a small challenge for myself in order to freshen up my JS.
I've been looking at various options, like Sylvester, MathJax and MathML.
Problem: However, none of the above appear to give functionality for using letters throughout my computation.
For instance, in Quantum Computation we often use multiply a matrix containing unknowns alpha and beta, with other matrices.
This is the sort of math I need to do:
http://i.stack.imgur.com/vH9Dk.gif
Ideally, I'd write this in the style of:
M=[[a],[b]], which of course, I cannot. Further, I'd be able to multiply to get "2*a" etc.
Any suggestions?
As suggested in the comments on the question, you could use strings. Then you just have to write your own matrix-matrix multiplication routine which will understand the difference between an entry containing a string and an entry containing a number.
However, as soon as you do more than one of these, you'll end up with expressions as well as variables and numbers. So we can generalise this to make every element be an expression. This is the beginnings of a symbolic algebra system as #High Performance Mark pointed out.
In javascript, I would guess that you want a set of expression objects, each implementing an interface including a method that returns whether the expression is determined or not yet. The gnarly bit is simplifying the resulting expressions to resolve the values of the variables.
Alternatively, do a bit more maths beforehand; move the variables out of the equations, and then let the code do the calculation.

How can dates and random numbers be used for evil in Javascript?

The ADsafe subset of Javascript prohibits the use of certain things that are not safe for guest code to have access to, such as eval, window, this, with, and so on.
For some reason, it also prohibits the Date object and Math.random:
Date and Math.random
Access to these sources of non-determinism is restricted in order to make it easier to determine how widgets behave.
I still don't understand how using Date or Math.random will accomodate malevolence.
Can you come up with a code example where using either Date or Math.random is necessary to do something evil?
According to a slideshow posted by Douglas Crockford:
ADsafe does not allow access to Date or random
This is to allow human evaluation of ad content with confidence that
behavior will not change in the future. This is for ad quality and
contractual compliance, not for security.
I don't think anyone would consider them evil per se. However the crucial part of that quote is:
easier to determine how widgets behave
Obviously Math.random() introduces indeterminism so you can never be sure how the code would behave upon each run.
What is not obvious is that Date brings similar indeterminism. If your code is somehow dependant on current date it will (again obviously) work differently in some conditions.
I guess it's not surprising that these two methods/objects are non-functional, in other words each run may return different result irrespective to arguments.
In general there are some ways to fight with this indeterminism. Storing initial random seed to reproduce the exact same series of random numbers (not possible in JavaScript) and supplying client code with sort of TimeProvider abstraction rather than letting it create Dates everywhere.
According to their website, they don't include Date or Math.random to make it easier to determine how third party code will behave. The problem here is Math.random (using Date you can make a psuedo-random number as well)- they want to know how third party code will behave and can't know that if the third party code is allowed access to random numbers.
By themselves, Date and Math.random shouldn't pose security threats.
At a minimum they allow you to write loops that can not be shown to be non-terminating, but may run for a very long time.
The quote you exhibit seem to suggest that a certain amount of static analysis is being done (or is at least contemplated), and these features make it much harder. Mind you these restrictions aren't enough to actually prevent you from writing difficult-to-statically-analyze code.
I agree with you that it's a strange limitation.
The justification that using date or random would make difficult to predict widget behavior is of course nonsense. For example implement a simple counter, compute the sha-1 of the current number and then act depending on the result. I don't think it's any easier to predict what the widget will do in the long term compared to a random or date... short of running it forever.
The history of math has shown that trying to classify functions on how they compute their value is a path that leads nowhere... the only sensible solution is classifying them depending on the actual results (black box approach).

Categories

Resources