I am new in math js library and I was trying to solve this expression:
var x_res = math.simplify('(x-'+x1+')^2 + ('+y_part+' - '+y1+')^2 - 197.5^2');
With simplify method i simplified it, but how can I do to know the "x" value ?
Thanks in advance.
I'm not sure what you mean by know the x value but you get an expression with one variable - x.
x1 = 2
y_part = 3
y1 = 4
var x_res = math.simplify('(x - '+x1 + ')^2 + (' + y_part + ' - ' + y1 + ')^2 - 197.5^2');
x_res.toString()
// "-156021 / 4 + (x - 2) ^ 2"
If you want then to evaluate the expression against a defined x, you can:
x_res.eval({ x: 1 })
// -39004.25
x_res.eval({ x: 2 })
// -39005.25
x_res.eval({ x: 1000 })
// 956998.75
Not regarding mathjs but, If you want to find what x will be equals to when the all equation equals some value you can use AlgebraJs
var expr = new Expression("x");
expr = expr.subtract(3);
expr = expr.add("x");
console.log(expr.toString());
2x - 3
var eq = new Equation(expr, 4);
console.log(eq.toString());
2x - 3 = 4
var x = eq.solveFor("x");
console.log("x = " + x.toString());
x = 7/2
Related
Here is the excel formula:
=E2-(J2*2*G2)-(K2*E2*2)/30+IF((L2+M2)>60,((L2+M2)-60)*H2+60*G2,(L2+M2)*G2)+(N2/$AE$1)*$AE$2+(Q2*$AE$5+P2*$AE$4+O2*$AE$3)
Here what I tried (Javascript):
var E2 = $("#sentence").val();
var J2 = $("#deduction").val();
var G2 = 55145;
var K2 = $("#absence").val();
var L2 = $("#overtime").val();
var M2 = 0;
var H2 = 50050;
var N2 = $("#transportation").val();
var sixty;
if ((L2 + M2) > 60) {
sixty = ((L2 + M2) - 60) * H2 + 60 * G2;
} else {
sixty = (L2 + M2) * G2;
};
var result = E2 - (J2 * 2 * G2) - (K2 * E2 * 2) / 30 + sixty;
I couldn't find the way to conver this part of formula:
+(N2/$AE$1)*$AE$2+(Q2*$AE$5+P2*$AE$4+O2*$AE$3)
Here I found the problem:
Even if one of the variables sets to null, then the formula does not work properly.
It looks like some pretty basic math.
let total = (N2 / AE1) * AE2 + (Q2 * AE5 + P2 * AE4 + O2 * AE3 )
This is basically impossible to translate without seeing the actual spreadsheet but that should get you started. Also, make sure to take into consideration order of operations because the computer is going to evaluate it from left to right unless there are parenthesis (where it will evaluate those first).
var m=10;
n=(m++)+(++m)+(m++)+(++m);
Output is
n=48 m=14
How does this answer occur?
Initially m=10. Now when ++ is executed, it will add 1 to the variable. In your code, there are 4 occurences of either ++m or m++, so it will be 14 at the end.
When ++ appears before the variable like ++m, it will add 1 first and use the variable in code. But when it appears after the variable like m++, the variable will be used first and then add 1.
So in n=(m++)+(++m)+(m++)+(++m);,
m++ - m will be used in code as 10 and becomes 11.
++m - m will be incremented by 1 first, so 11+1 =12 and will be used in code.
m++ - m will be used in code as 12 and becomes 13.
++m - m will be incremented by 1 first, so 13+1 =14 and will be used in code.
So, final result will look like this:
n=10+12+12+14
var m = 10;
var m1 = m++;
var m2 = ++m;
var m3 = m++;
var m4 = ++m;
var n = m1 + m2 + m3 + m4;
console.log('n: ' + n);
console.log('m: ' + m);
console.log('(m++): ' + m1 + ', (++m): ' + m2 + ', (m++): ' + m3 + ', (++m): ' + m4);
console.log('---');
var x = 10;
var y = (x++) + (++x) + (x++) + (++x);
console.log('y: ' + y);
console.log('x: ' + x);
m++ this will use previous value of m then increment.
++m this will first increment and then use the incremented value of m.
now,
n=(m++)+(++m)+(m++)+(++m);
-> m++ here the value of m will be first used then increased by 1. so expression will be 10 + (++m) + (m++) + (++m). and for further value of m will be 11.
-> for next it is ++m so it will first increment then use in expression. so previously the value of m was 11, now 11+1 is 12. so expression will look like 10+12 + (m++) + (++m).
-> similarly to first point here also it will first used then increment. so expression will be n=10+12+12+(++m). but after this the value of m is now 13.
-> so here (++m) it will first increment and then add. so previously the value was 13 so 13+1=14. so m is 14 and expression will be n=10+12+12+14.
so the value of n=48 and m=14
I need to make the function so that it calculate the area when I enter function f and coefficients a, b and n on the command line.
For now I have this:
module.exports = function (f,a,b,n,next) {
parseFloat(a);
parseFloat(b);
parseInt(n);
var h = (b-a)/n;
var s = 0;
var suma = function () {
for (var i = 0; i <= n; i++) {
var ximj = a+h*(i-1);
var xi = a+h*i;
var x = (ximj + xi)/2;
s += eval(f,x);
}
return s;
};
if (n<1) {
next(new Error("Koeficijent n je broj ekvidistantnih točaka."))
}
else next(null, {povrsina : function () {return h*suma();}}
);
I think that the function suma() isn't working like it should be working.
In command line this should be look like :
f: x*x-2*x+7
a: 2
b: 4
n: 10
Povrsina ispod grafa funkcije f je 20.66000...
My prompt file looks like this:
var pov = require('./integrator.js');
var prompt = require('prompt');
prompt.get (['f','a','b','n'],function (err,koef) {
if (err) return console.log(err);
console.log("f: ",koef.f);
console.log("a: ",koef.a);
console.log("b: ",koef.b);
console.log("n: ",koef.n);
parseFloat(koef.a);
parseFloat(koef.b);
parseInt(koef.n);
pov(koef.f,koef.a,koef.b,koef.n,function(err,rj){
if (err)
console.log(err);
else
console.log("Povrsina ispod grafa funkcije f je " + rj.povrsina());
});
Thank you for help :)
First of all, if you want to calculate the area under a curve you are going to need to provide a lower and upper x limit, otherwise the answer will always be infinite. Personally I use this function that I wrote:
function findArea(lowerLimit, upperLimit, coefficients) {
let lower = 0
let upper = 0
for (let i = 0; i < coefficients.length; i++) {
lower += (coefficients[i] * Math.pow(lowerLimit, i + 1)) / (i + 1)
upper += (coefficients[i] * Math.pow(upperLimit, i + 1)) / (i + 1)
}
return Math.abs(upper - lower)
}
If you pass in two numbers as the limits and an array of coefficients in the format, a + bx + cx^2 + dx^3 ... and so on, you will get the correct answer. For example, say you have the equation y = 5 + 4x + 3x^2 + 2x^3 and you want to find the area under the curve between x = 1 and x = 3 you can call this function like so, findArea(1, 3, [5, 4, 3, 2]) and you will get the answer 92 which represents the number of units squared. Keep in mind that this works only if the area you are evaluating is either entirely above or entirely below the x axis. If your area crosses the x axis you will need to calculate the area above and below separately!
You can check the results against the Symbolab Area Under The Curve Calculator but this should work for any curve.
The eval function reads a string and evaluates it. If you want to evaluate the function f with the parameter x, then you can just write f(x) instead of calling eval(). This is because, in JavaScript, functions are first-class values and can be passed around just like numbers, strings, and so on.
In suma, particularly the line var ximj = a+h*(i-1);, your for-loop multiplies h by -1, then by 0, then by 1. I suspect you meant var ximj = a+h*(i+1); since h is the width of your differential. But that would only produce small errors.
As Lends wrote, you also need to correct your Parse* calls:
a = parseFloat(a);
b = parseFloat(b);
n = parseInt(n);
You should use math.js to eval a string math expression, for example:
var math = require('mathjs');
var integral = function(f, a, b, n, next) {
a = parseFloat(a);
b = parseFloat(b);
n = parseInt(n);
var h = (b - a) / n;
var s = 0;
var suma = function() {
for (var i = 0; i <= n; i++) {
var ximj = a + h * (i - 1);
var xi = a + h * i;
var x = (ximj + xi) / 2;
s += math.eval(f, { x: x });
}
return s;
};
if (n < 1) {
next(new Error("Koeficijent n je broj ekvidistantnih točaka."))
} else {
next(null, { povrsina: function() { return h * suma(); } });
}
};
var f = 'x * x - 2 * x + 7';
var a = '2';
var b = '4';
var n = '10000';
integral(f, a, b, n, function(err, rj) {
if (err)
console.log(err);
else
console.log("Povrsina ispod grafa funkcije f je " + rj.povrsina());
});
/* output:
Povrsina ispod grafa funkcije f je 20.66806662000201
*/
You integral function seems to have an has an issue though, n must be very high to get near the expected result (like 10000). Or is it supposed to work that way? For n = 10, it gives 22 for example.
Another solution is to use an existing package or at least read the source code to get an idea.
I am trying to make a simple quadratic formula solver for my own personal use. It works for the most part, but there's one problem: it only works when the answers are Rational numbers (i.e., it won't display sqrt(-1) because that's "i"). When it tries to perform the calculation and the answer isn't a rational, it will display "NaN". My code looks like this:
...*regular html*
<script type = "text/javascript">
var aValue = prompt("What is your 'a' value?");
var bValue = prompt("What's your 'b' value?");
var cValue = prompt("What's your 'c' value?");
var quadFinder = function x_finder(a,b,c) {
document.write((-1 * b + Math.sqrt(b*b - 4*a*c)) / 2*a);
document.write("<br>");
document.write((-1 * b - Math.sqrt(b*b - 4*a*c)) / 2*a);
};
quadFinder(aValue,bValue,cValue)
I know the function is all sound because it will work as long as the answer is only a number.
One other question: what is the Math. command that will round the number? I once put in a few numbers and it came out to some crazy number with around 10 decimal numbers after it.
You might try looking at the discriminant and catching complex cases:
var quadFinder = function x_finder(a,b,c) {
var disc = b * b - 4 * a * c;
if (disc >= 0){
document.write((-1 * b + Math.sqrt(b*b - 4*a*c)) / 2*a);
document.write("<br>");
document.write((-1 * b - Math.sqrt(b*b - 4*a*c)) / 2*a);
} else {
var real = (-1 * b) / (2 * a);
var complex = Math.sqrt(-disc)/(2 * a);
document.write(real + " + " + complex + "i");
document.write("<br>");
document.write(real + " - " + complex + "i");
};
First I test that every variable got a number value:
09-11 18:15:00.420:
d_drop: -1.178791867393647
drop_at_zero: 0.0731037475605623
sightHeight: 4.5
d_distance: 40
zeroRange: 10
09-11 18:15:00.420:
d_drop: true
drop_at_zero: true
sightHeight: true
d_distance: true
zeroRange: true
function isNumber (o) {
return ! isNaN (o-0) && o != null;
}
var d_drop; // in calculation this gets value 1.1789
var d_path = -d_drop - sightHeight + (drop_at_zero + sightHeight) * d_distance / zeroRange;
console.log("Path: " + d_path + " cm");
and in the log:
09-11 18:15:00.430: D/CordovaLog(1533): Path: NaN cm
WHY? I have tried to figure that out couple of hours now and no success, maybe someone has an idea, I haven't!
Thanks!
Sami
-------ANSWER IS that parse every variable when using + operand-----------
var d_path = parseFloat(-d_drop) - parseFloat(sightHeight) + (parseFloat(drop_at_zero) + parseFloat(sightHeight)) * parseFloat(d_distance) / parseFloat(zeroRange);
The addition operator + will cast things as strings if either operand is a string. You need to parse ALL of your inputs (d_drop, sightHeight, etc) as numbers before working with them.
Here's a demo of how the + overload works. Notice how the subtraction operator - is not overloaded and will always cast the operands to numbers:
var numberA = 1;
var numberB = 2;
var stringA = '3';
var stringB = '4';
numberA + numberB // 3 (number)
numberA - numberB // -1 (number)
stringA + stringB // "34" (string)
stringA - stringB // -1 (number)
numberA + stringB // "14" (string)
numberA - stringB // -3 (number)
http://jsfiddle.net/jbabey/abwhd/
At least one of your numbers is a string. sightHeight is the most likely culprit, as it would concatenate with drop_at_zero to produce a "number" with two decimal points - such a "number" is not a number, hence NaN.
Solution: use parseFloat(varname) to convert to numbers.
If you're using -d_drop as a variable name, that is probably the culprit. Variables must start with a letter.
var d_drop = -1.178791867393647,
drop_at_zero = 0.0731037475605623,
sightHeight = 4.5,
d_distance = 40,
zeroRange = 10;
var d_path = d_drop - sightHeight + (drop_at_zero + sightHeight) * d_distance / zeroRange;
console.log("Path: " + d_path + " cm"); // outputs: Path: 12.613623122848603 cm