plot sin + 5 in javascript

To plot the sin(x) + 5 function in JavaScript, you can use a library like Chart.js or D3.js. Here's an example using Chart.js:

First, you'll need to include the Chart.js library in your HTML file:

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
  <canvas id="myChart"></canvas>

  <script>
    // Define the x values
    const xValues = [];
    for (let x = 0; x <= 2 * Math.PI; x += 0.1) {
      xValues.push(x);
    }

    // Calculate the y values
    const yValues = xValues.map(x => Math.sin(x) + 5);

    // Create a chart
    const ctx = document.getElementById('myChart').getContext('2d');
    const chart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: xValues,
        datasets: [{
          label: 'y = sin(x) + 5',
          data: yValues,
          borderColor: 'blue',
          borderWidth: 1
        }]
      },
      options: {
        responsive: true,
        scales: {
          x: {
            title: {
              display: true,
              text: 'x'
            }
          },
          y: {
            title: {
              display: true,
              text: 'y'
            }
          }
        }
      }
    });
  </script>
</body>
</html>
1059 chars
53 lines

Save the HTML file and open it in a web browser. You should see a line chart displaying the sin(x) + 5 function. The x-axis represents the values of x, and the y-axis represents the values of y. The chart is labeled with the equation.

Please note that you'll need an internet connection to load the Chart.js library from the CDN.

related categories

gistlibby LogSnag