matlab code for ploting the trajectories of hyperbola parabola and ellipse in javascript

To plot the trajectories of hyperbola, parabola and ellipse using MATLAB code in JavaScript, you can use the following steps:

  1. Define the equations for the three curves.
index.tsx
// Equation of a hyperbola
// (x-a)^2/a^2 - (y-b)^2/b^2 = 1
function hyperbola(a,b,x_start,x_end,step){
  let x = [];
  let y = [];
  for(let i=x_start; i<=x_end; i+=step){
    x.push(i);
    y.push((b/a)*Math.sqrt((i-a)**2-1)+b);
    y.push(-(b/a)*Math.sqrt((i-a)**2-1)+b);
  }
  return {
    x: x,
    y: y
  };
}

// Equation of a parabola
// y = a*x^2 + b*x + c
function parabola(a,b,c,x_start,x_end,step){
  let x = [];
  let y = [];
  for(let i=x_start; i<=x_end; i+=step){
    x.push(i);
    y.push(a*i*i+b*i+c);
  }
  return {
    x: x,
    y: y
  };
}

// Equation of an ellipse
// (x-a)^2/a^2 + (y-b)^2/b^2 = 1
function ellipse(a,b,x_start,x_end,step){
  let x = [];
  let y = [];
  for(let i=x_start; i<=x_end; i+=step){
    x.push(i);
    y.push(b*Math.sqrt(1-((i-a)**2)/(a**2)));
    y.push(-b*Math.sqrt(1-((i-a)**2)/(a**2)));
  }
  return {
    x: x,
    y: y
  };
}
881 chars
47 lines
  1. Define the parameters for each curve and call the corresponding function.
index.tsx
// Hyperbola
let a_h = 3;
let b_h = 2;
let x_start_h = -10;
let x_end_h = 10;
let step_h = 0.1;
let hyperbola_data = hyperbola(a_h,b_h,x_start_h,x_end_h,step_h);

// Parabola
let a_p = 1;
let b_p = 0;
let c_p = 0;
let x_start_p = -10;
let x_end_p = 10;
let step_p = 0.1;
let parabola_data = parabola(a_p,b_p,c_p,x_start_p,x_end_p,step_p);

// Ellipse
let a_e = 4;
let b_e = 2;
let x_start_e = -10;
let x_end_e = 10;
let step_e = 0.1;
let ellipse_data = ellipse(a_e,b_e,x_start_e,x_end_e,step_e);
496 chars
25 lines
  1. Use a plotting library (e.g. Plotly) to plot the data.
index.tsx
// Hyperbola plot
let trace1 = {
  x: hyperbola_data.x,
  y: hyperbola_data.y,
  type: 'scatter'
};
let layout1 = {
  title: 'Hyperbola',
  xaxis: {
    title: 'x'
  },
  yaxis: {
    title: 'y'
  },
  width: 500,
  height: 500
};
Plotly.newPlot('hyperbola', [trace1], layout1);

// Parabola plot
let trace2 = {
  x: parabola_data.x,
  y: parabola_data.y,
  type: 'scatter'
};
let layout2 = {
  title: 'Parabola',
  xaxis: {
    title: 'x'
  },
  yaxis: {
    title: 'y'
  },
  width: 500,
  height: 500
};
Plotly.newPlot('parabola', [trace2], layout2);

// Ellipse plot
let trace3 = {
  x: ellipse_data.x,
  y: ellipse_data.y,
  type: 'scatter'
};
let layout3 = {
  title: 'Ellipse',
  xaxis: {
    title: 'x'
  },
  yaxis: {
    title: 'y'
  },
  width: 500,
  height: 500
};
Plotly.newPlot('ellipse', [trace3], layout3);
824 chars
57 lines

This will plot the trajectories of hyperbola, parabola and ellipse in separate divs with Ids hyperbola, parabola and ellipse. You can customize the plots by modifying the parameters and layouts.

gistlibby LogSnag