M.K
M.K

Reputation: 11

Exponential decrease

I am stuck getting an exponential decrease from 100 to 0. This is what I managed to get:

var time_data = [];
var value_data = [];
var left = 100;
var  i = 1;

while (i <= 30) 
{
	left = left-Math.pow(i,i);
	
	if(left < 0)
	{
		left = 0;

	}
	
	value_data.push(left);
	time_data.push(i);
	
	i+=1;
}

var canvas = document.getElementById('myChart');
var data = {
    labels: time_data,
    datasets: [
        {
            label: "My First dataset",
            fill: false,
            lineTension: 0.1,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 5,
            pointHitRadius: 10,
            data: value_data,
        }
    ]
};

var option = {
	showLines: true
};
var myLineChart = Chart.Line(canvas,{
	data:data,
  options:option
});

function adddata(){
	myLineChart.data.datasets[0].data[7] = 50;
  myLineChart.data.labels[7] = "test add";
  myLineChart.update();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.min.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>

while (i <= 30) 
{
    left = left-Math.pow(i,i);

    if(left < 0)
    {
        left = 0;

    }

And this is what I need to get

enter image description here

The problem is that don't know how to get a reverse version of Math.pow.

I would appreciate any help.

Upvotes: 1

Views: 3663

Answers (2)

RaphaMex
RaphaMex

Reputation: 2839

According to the graph, what you want is to start at (0,10) and then divide y by 2 every time x is incremented by 1.

Try this:

var time_data = [];
var value_data = [];

for (let i=0;i<=10;++i) {
    time_data.push(i);
    value_data.push(10/Math.pow(2,i));
}

This can be mathematically written as a decreasing exponential: 10 * 2^(-x) or if you prefer 10 * exp(-ln(2) * x)

Upvotes: 3

Alex Sage
Alex Sage

Reputation: 45

If you're looking for exponential decay, do you need to use Math.pow()? What's wrong with something like

left = left*factor

Where 0 < factor < 1?

Looking at some documentation for Math.pow(), the two arguments are base and exponent. In your example, left - 30^30 doesn't make much sense because it will result in a vastly negative number and then default to 0.

An expression using Math.pow() that will give the ith term of an exponential decay sequence is

start_value * Math.pow(factor, i)

Where again, 0 < factor < 1. The reduction in the value that you start with is compounded exponentially over each iteration.

Upvotes: 0

Related Questions