I'm fairly new to PHP and I'm trying to design a car's gear ratio calculator that takes certain vehicle's dimensions as input. One way to calculate them is trough using geometric progression method. Below are the codes I'm trying to run:
<?
$alowgearf = 14.37;
$ahigear = 3.293;
$noOfGear = 5;
$loopLimit = $noOfGear - 2;
$gears = array(0 => "$alowgearf", "$noOfGear - 1" => "$ahigear");
$nlnh = $alowgearf/$ahigear;
$cgp = pow($nlnh, 1/($noOfGear - 1));
for ( $n = 1; $n <= $loopLimit ; $n++ ) {
$m = $n - 1;
$gear = $gears[$m]/$cgp;
$gears[$n] = $gear;
}
print_r($gears);
?>
The formula for the GPM method of calculating gear ratio is as follows :
N[i+1] = N[i]/Cgp,
where N is gear ratio, i is the gear number and Cgp is the geometric progression constant.
By supplying the formula with the lowest and highest gear ratio and the desired number of gears, we can calculate the rest of the gear ratios.
The problem is, the codes above outputs nothing. I've been trying to fix the code but I can't detect any problem with them. I've been toying with other loop function such as do-while but the results are the same. What's the problem? What I've been doing wrong?
Related
I'm trying to calculate the directional movement indicator for 5m interval using the API of Binance. I'm using 288 values of "High", "Open" and "Close" and
I'm calculating the True range and then the Average True Range (ATR) with a simple moving average of a window with 14 values. I'm using the same Simple Moving average technique
for the calculation of the +DI, -DI and the ADX, but the values that I get don't match the ones that are shown in trading view for the DMI. I have also tried using an
exponential moving average the ATR, the +Di and the -DI, but I still don't get matching values. I have also noticed that the DMI of Binance and Trading view do not match.
Do you know which smoothing techinique is used by the DMI of trading view?
$url = 'https://api.binance.com/api/v3/klines?symbol=BNBBTC&interval=5m&limit=288';
$candles = file_get_contents($url);
$candles = json_decode($candles, true);
$arr_results = array();
$high = array();
$low = array();
$close_arr = array();
$average = array();
for($i= 0; $i < sizeof($candles); $i++){
array_push($high, $candles[$i][2]);
array_push($low, $candles[$i][3]);
array_push($close_arr, $candles[$i][4]);
$av = ($candles[$i][2] + $candles[$i][3]) /2;
array_push($average, $av);
}
$plus_di = array_pop(trader_plus_di($high, $low, $close_arr, 14));
$minus_di = array_pop(trader_minus_di($high, $low, $close_arr, 14));
$adx = array_pop(trader_adx ($high, $low, $close_arr, 14));
To calculate the ATR, you need to smooth the TR (True Range) according to the following method :
Take a smoothing period nbCandles (nbCandles=14 candles typically), then:
First ATR value: ATR(1) = Sum{TR(1 -> nbCandles)} (i.e you sum the nbCandles first values of the TR to get your first value of the ATR) - If nbCandles = 14 then your first ATR value will be the sum of the first 14 values of TR.
Then, for the following ATR values, you use the following formula:
ATR(i) = ATR(i-1) - ATR(i-1)/nbCandles + TR(i)
Here is an example (based on 30 1 minutes candles historical record and 14 as the smoothing factor (number of candles used for smoothing)) :
Binance BTCUSDT 1m
Calculation of boxes / Volume / Size
I need to put m boxes of n different sizes inside another rectangular box!
What is the calculation to obtain the smallest box (with the lowest volume possible) based on the m boxes of n different sizes?
I know the size of the boxes m, and also know how many are.
Keep in mind that for example:
two boxes with 3 x 3 x 3
only better organized in a box 6 x 3 x 3
and the formula works well:
sum of the smallest measure X the largest measure X the other major measure
Another example is two boxes:
4 X 5 X 7
6 X 8 X 2
sum of the smallest measure = (4 + 2) X (8) X (7)
However, when we have 10 boxes for example, this does not work at all!
How to organize, and what is the volume of the smallest possible box based on the dimensions of the boxes I have?
I do not know if this question can only be answered with basic tools (I tried via optimization (Derivative) and I could not)
I need to make a calculation via PHP from mailboxes! And I can not solve the problem Mathematically before transforming into code
I don't have problems with PHP code, i have problems with Math, i don`t know how to solve this problem with the best way possible!
I have used the code below, but as explained, it will not work for a lot of boxes, only for 2 or 3.
$length = 0;
$height = 0;
$width = 0;
foreach ($products as $key => $product) {
for ($quantity = 0; $quantity < $product['quantity']; $quantity ++) {
$length = $length + (float) $product['dimension'][0];
}
if($product['dimension'][1] > $height) {
$height = (float) $product['dimension'][1];
}
if($product['dimension'][2] > $width) {
$width = (float) $product['dimension'][2];
}
}
$box = ($length . ' x '. $height . ' x '. $width);
I'm kind of stuck with this, it SHOULD be very simple, but my brain can't wrap around it (it's Friday... lol)
I've got a thermometer, representing $1,000,000 max. it's 375px tall.
I'm running a DB Query to grab amounts from user submissions (between $1 and $200).
At that math, it's $2,666.66 per pixel to move it up 1 pixel ---
retrieve_amount(); is my DB function that grabs all the amounts - that's simple.
$fill_query = retrieve_amount();
$fill = 0;
$total = 0;
while($fill_query->is_valid() ) : $fill_query->amount();
$amount = get_valid_amount($input, 'amount');
$total = $total + $amount;
endwhile;
$finaltotal = $total; // THIS is the line that grabs the final total from above. Should work?
$fillheight = $SOMETHING +/-* $SOMETHING; // this is the line that i'm less sure of how to get my result
It may be that I'm just not great with math, but my questions are
$finaltotal = $total
should work to receive the total amount retrieved from the DB Query, correct?
And more importantly, how do I translate that to the pixels that I need?
$maxPixels = 375;
$maxAmount = 1000000;
$currentAmount = 1234567;
$currentPixels = round(($currentAmount / $maxAmount) * $maxPixels);
It's basically just like calculating percentages. Except, instead of 100%, your max is now 375 pixels.
I want to display a color between red, yellow, green depending on a number between 1 to 100.
1 being green and 100 being red, 50 being yellow. I want to basically create a gradient between that.
So far, I tried:
$r = floor(255 * ($number / 100));
$g = 255 - $r;
And it somewhat does it, but gives me brownish & dark colors, & no yellow at all.
It's because you shouldn't change both channels at once but rise R in the first half and lower G in the second.
Try a function like this:
function GreenYellowRed($number) {
$number--; // working with 0-99 will be easier
if ($number < 50) {
// green to yellow
$r = floor(255 * ($number / 50));
$g = 255;
} else {
// yellow to red
$r = 255;
$g = floor(255 * ((50-$number%50) / 50));
}
$b = 0;
return "$r,$g,$b";
}
To test it:
$output = "";
for ($i = 1; $i <= 100; $i++) {
$rgb = GreenYellowRed($i);
$output .= "<div style='background-color: rgb($rgb)'>$rgb</div>";
}
echo $output;
I've found that dealing with the HSV color model is easier than the RGB model. It helps you easily choose the color you want to work with; with RGB you'd need to understand how different values of R, G and B will combine to give you the color you want/don't want.
Also, this SO question might be useful: How can I cycle through hex color codes in PHP?
I don't know of a mathematical model for a "color curve" that passes through specified RGB color values (e.g. what you describe as green/yellow/red), which would allow you to calculate any intermediate color in that curve. In any case, a model of a function (which is what that would be) is only as good as the data points it needs to fit, so you 'd have to be much more specific than green/yellow/red to get decent results even if someone points out the math.
Remember that we are not interested in mathematical interpolation here, but rather in "color-space interpolation" (a term which I just made up) -- in other words, what would look like a "natural" interpolation to a human.
An easier solution for those of us who do not have the necessary color theory knowledge, and which I 'd suggest, is to pre-select a number of colors with a color picker tool, divide the 0-100 range into as many bands as the colors you picked, and use simple integer division to project from 0-100 to a color band.
Food for thought: Indeed, how does SO decide the color of the upvote count for comments?
Update: I just asked the above over on meta. Let's see...
After a bit of looking, none of the solutions looked pleasing. As stated above, HSV is probably the way to go, since modern browsers can render color with it just fine.
To get a good idea of the colors you are working with, check out this color wheel:
http://www.colorspire.com/rgb-color-wheel/
I want to start with blue, so I use 255 for normalization.
function temp_color($temp){
$start = 40;
$end = 85;
$normal = round(255-((($temp - $start)/($end-$start))*255));
$color = "hsl($normal, 100%, 30%);";
$span = "<span style=\"color: $color\">$temp</span>";
return $span;
}
I have a problem drawing different functions with PHP (GD, of course).
I managed to draw different functions but whenever the parameters of the function change - the function floats wherever it wants.
Let us say that I have a first function y=x^2 and I have to draw it from -5 to 5. This means that the first point would be at (-5;25). And I can move that to whatever point I want if I know that. But if I choose y=2x^2 with an interval x=(-5;5). The first point is at (-5;50). So I need help in calculating how to move any function to, let's say, (0;0).
The functions are parabola/catenary alike.
What you want to do is find the maximum boundaries of the graph you are making. To do this you have to check each inflection point as well as the range bounds. Store each coordinate pair in an array
Part 1 [Range Bounds]:
Collect the coordinates from the range bounds.
<?php
$ybound[] = f($minX);
$ybound[] = f($maxX);
Part 2 [Inflections]:
This part is more difficult. You can either have a series of equations to solve for inflections for each type of parabola, or you can just brute force it. To do this, just choose a small increment, (what ever your small increment is for drawing the line), I will use 0.1
<?php
for($x = $minX; $x <= $maxX; $x += 0.1) {
$ybound[] = f($x);
}
Note, if you brute force, you can skip Part 1, otherwise, it would be faster if you could figure out the inflections for the scope of your project
Part 3 [Min Max]:
Now you get the min and max values from the array of possible y values.
<?php
$minY = min($ybound);
$maxY = max($ybound);
Part 4 [Shift]:
Now that you have this, it should be very simple to adjust. You take the top left corner and set that to 0,0 by adjusting each new coordinate to that value.
<?php
$shiftX = -$minX;
$shiftY = $maxY;
With this info, you can also determine your image size
<?php
$imageX = $maxX - $minX;
$imageY = $maxY - $minY;
Then as you generate your coordinates, you will shift each one, by adding the shift value to the coordinate.
<?php
for($x = -$minX; $x <= $maxX; $x += 0.1) {
$ycoor = $shiftY - f($x);
$xcoor = $x + $shiftX;
//draw ...
}
Drawing the axis is also easy,
<?php
$xaxis = $shiftY;
$yaxis = $shiftX;
(I think I have all my signs correct. Forgive me if they are off)
You first need to determine the bounding box of your function. Then, you calculate the width and the height, and you normalize so it fits into a rectangle whose top left coordinate is (0,0). Maybe you will also need to scale the figure to get it at a specific size.