Javascript - Find the base of an exponential equation - javascript

I was wondering if anyone knew how to find the base of an exponential equation in Javascript.
I couldn't find any function that allows this to be done (e.g. using the Math functions).
For example, how do I find 'b' in the following equation:
y = b^t
Thanks in advance for any help you can provide.

If you know what are the values of y and t are, you can get the value of b by calculating the t-th root of y like this:
Math.pow(y, 1/t);
Source:
JavaScript: Calculate the nth root of a number

What you need is math and the logarithm.
y = b^t
=> t = log(y) / log(b)
=> log(b) = log(y) / t
=> b = 10 ^ ( log(y) / t )
So it would be something like
b = Math.pow(10, (Math.log(y) / t));
-Hannes

Related

What is `r=r=>` in Processing intended to do in this simulation?

I am trying to understand superficially the syntax in this pretty piece of coding posted here:
t=0,draw=e=>{for(t++||createCanvas(w=400,w,WEBGL),background(w),fill(x=0),rotateX(d=t/120),rotateY(d),rotateZ(d);x<1331;)push(r=r=>(r-5)*(10+w*(o=1-sin(t%120/240*PI)))),translate(r(a=x%11),r(b=x%121/11|0),r(c=x++/121|0)),pop(box(10+o*(666==x?90:-10)))};//
I think that because of Twitter's constraints in number of characters it was posted as one single line, and using some shortcuts for loops. The part I am asking about may be a loop. I tried unfolding the code into something more readable as follows:
t=0,draw=e=>{for(t++||
createCanvas(w = windowWidth,w,WEBGL), background(w), fill(x=0),
rotateX(d=t/120),
rotateY(d),
rotateZ(d);
x<1331;)
push(r=r=>(r-5) * (10 + w * (z = 1 - sin(t%120/240*PI)))),
translate(r(a=x%11),
r(b=x%121/11|0),
r(c=x++/121|0)),
pop(box(10+z*(666==x?90:-10)))};//
What is r=r=>(r-5) purpose? I have looked up push() and pop(), and I have a vague idea, but that expression is confusing.
This whole callback starts at t=0,draw=e=>
This e is probably an event, the letter e is often used for event
this is a callback function, which is assigned to 'r'
r = (r) => (r * 5) * (10 + w * (z = 1 - sin(t%120/240*PI)))),
translate(r(a=x%11),
r(b=x%121/11|0),
r(c=x++/121|0)),
pop(box(10+z*(666==x'90:-10))};
it's something like
number = (num) => num * 2
number(4) // will multiply four by two

How to make an output Absolute Value in Javascript? [duplicate]

I want to get the absolute value of a number in JavaScript. That is, drop the sign.
I know mathematically I can do this by squaring the number then taking the square root, but I also know that this is horribly inefficient.
x = -25
x = x * x
x = Math.sqrt(x)
console.log(x)
Is there a way in JavaScript to simply drop the sign of a number that is more efficient than the mathematical approach?
You mean like getting the absolute value of a number? The Math.abs javascript function is designed exactly for this purpose.
var x = -25;
x = Math.abs(x); // x would now be 25
console.log(x);
Here are some test cases from the documentation:
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN
Here is a fast way to obtain the absolute value of a number. It's applicable on every language:
x = -25;
console.log((x ^ (x >> 31)) - (x >> 31));
If you want to see how JavaScript implements this feature under the hood you can check out this post.
Blog Post
Here is the implementation based on the chromium source code.
function MathAbs(x) {
x = +x;
return (x > 0) ? x : 0 - x;
}
console.log(MathAbs(-25));
I think you are looking for Math.abs(x)
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/abs
Alternative solution
Math.max(x,-x)
let abs = x => Math.max(x,-x);
console.log( abs(24), abs(-24) );
Also the Rick answer can be shorted to x>0 ? x : -x
to simply drop the sign of a number
var x = -25;
+`${x}`.replace('-', '');

How do I find the intersection of two lines in JavaScript, given their functions?

I realize this is more of a math question, but I don't have a math brain. I'm specifically interested in solving this problem in JavaScript, just for this specific case. If someone can do this for me with a simple function, I can generalize the solution myself as needed.
I have two lines on a graph. One is linear, just going straight from (0,0) to (x,x):
// Line 1
var f1 = function(x) {
return x;
};
The other line is curved, and can be drawn like this:
// Line 2
var f2 = function(x) {
var alpha = 0.3;
return (1 - alpha) *
(1.4 *
1.6 ** alpha) *
(x ** -alpha);
};
Given only these functions, can I write a function that gives me the co-ordinates of the point(s) at which these two lines intersect?
I've looked at things like algebra.js, but haven't been able to come up with the solution myself.
Derivating process:
So, for alpha= a= 0.3 we have:
k= (1-alpha) * 1.4 * 1.6**alpha = 1.12839738
x= k ** (1/(1 + alpha)) = 1.09737595
The desired intersection is {x, f(x)} = {1.0974, 1.0974}

How would I write this formula in Javascript?

I have a project I am working on and it needs to calculate mortgage calculations but I'm having trouble putting the formula into javascript.
the formula is:
M = P I(1 + I)^n /(1 + I )^n - 1
Any help is appreciated, thanks
P = loan princible
I = interest
N = Term
Break it down into a sequence of steps.
Multiplication is as straightforward as it gets: I*(1+I)
Division is the same: I/(1+I)
To the power of n is denoted by: Math.pow(3, 5); //3 to the power of 5
Math.pow() might be the only thing you didn't know yet.
Unrelated but useful,
Wrap your formula into a function and you have a mortgage-calculation function
calculateMortgage(p,i,n) {
result = //translate the formula in the way I indicated above
return result;
}
and call it like so:
var mortgage = calculateMortgage(300,3,2); // 'mortgage' variable will now hold the mortgage for L=300, I=3, N=2
Also, the formula you posted really doesn't make any sense - why is there a blank between P & I at the very beginning? Something's missing.
Try this: Math.pow(p*i*(1+i),n)/Math.pow(1+i,n-1)
Math.pow(a,2) is same as a^2
if P is not supposed to be with numerator then
this
p * (Math.pow(i*(1+i),n)/Math.pow(1+i,n-1))
or
p * (Math.pow((i+i*i),n)/Math.pow(1+i,n-1))
var M;
var P;
var I;
M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);
Does this look right to you? I got the correctly styled formula from here.
Like what Nicholas above said, you can use functions to make it all the more easier.
var M;
function calculateMortgage(P, I, N){
M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);
alert("Your mortgage is" + M);
}
And just call calculateMortgage(100, 100, 100); with your values for it to automatically give the answer to you.

Get the absolute value of a number in Javascript

I want to get the absolute value of a number in JavaScript. That is, drop the sign.
I know mathematically I can do this by squaring the number then taking the square root, but I also know that this is horribly inefficient.
x = -25
x = x * x
x = Math.sqrt(x)
console.log(x)
Is there a way in JavaScript to simply drop the sign of a number that is more efficient than the mathematical approach?
You mean like getting the absolute value of a number? The Math.abs javascript function is designed exactly for this purpose.
var x = -25;
x = Math.abs(x); // x would now be 25
console.log(x);
Here are some test cases from the documentation:
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN
Here is a fast way to obtain the absolute value of a number. It's applicable on every language:
x = -25;
console.log((x ^ (x >> 31)) - (x >> 31));
If you want to see how JavaScript implements this feature under the hood you can check out this post.
Blog Post
Here is the implementation based on the chromium source code.
function MathAbs(x) {
x = +x;
return (x > 0) ? x : 0 - x;
}
console.log(MathAbs(-25));
I think you are looking for Math.abs(x)
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/abs
Alternative solution
Math.max(x,-x)
let abs = x => Math.max(x,-x);
console.log( abs(24), abs(-24) );
Also the Rick answer can be shorted to x>0 ? x : -x
to simply drop the sign of a number
var x = -25;
+`${x}`.replace('-', '');

Categories