HTML5画布绘图方程

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }

    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="300"></canvas>
    <script>
      function Graph(config) {
        // user defined properties
        this.canvas = document.getElementById(config.canvasId);
        this.minX = config.minX;
        this.minY = config.minY;
        this.maxX = config.maxX;
        this.maxY = config.maxY;
        this.unitsPerTick = config.unitsPerTick;

        // constants
        this.axisColor = '#aaa';
        this.font = '8pt Calibri';
        this.tickSize = 20;

        // relationships
        this.context = this.canvas.getContext('2d');
        this.rangeX = this.maxX - this.minX;
        this.rangeY = this.maxY - this.minY;
        this.unitX = this.canvas.width / this.rangeX;
        this.unitY = this.canvas.height / this.rangeY;
        this.centerY = Math.round(Math.abs(this.minY / this.rangeY) * this.canvas.height);
        this.centerX = Math.round(Math.abs(this.minX / this.rangeX) * this.canvas.width);
        this.iteration = (this.maxX - this.minX) / 1000;
        this.scaleX = this.canvas.width / this.rangeX;
        this.scaleY = this.canvas.height / this.rangeY;

        // draw x and y axis
        this.drawXAxis();
        this.drawYAxis();
      }

      Graph.prototype.drawXAxis = function() {
        var context = this.context;
        context.save();
        context.beginPath();
        context.moveTo(0, this.centerY);
        context.lineTo(this.canvas.width, this.centerY);
        context.strokeStyle = this.axisColor;
        context.lineWidth = 2;
        context.stroke();

        // draw tick marks
        var xPosIncrement = this.unitsPerTick * this.unitX;
        var xPos, unit;
        context.font = this.font;
        context.textAlign = 'center';
        context.textBaseline = 'top';

        // draw left tick marks
        xPos = this.centerX - xPosIncrement;
        unit = -1 * this.unitsPerTick;
        while(xPos > 0) {
          context.moveTo(xPos, this.centerY - this.tickSize / 2);
          context.lineTo(xPos, this.centerY + this.tickSize / 2);
          context.stroke();
          context.fillText(unit, xPos, this.centerY + this.tickSize / 2 + 3);
          unit -= this.unitsPerTick;
          xPos = Math.round(xPos - xPosIncrement);
        }

        // draw right tick marks
        xPos = this.centerX + xPosIncrement;
        unit = this.unitsPerTick;
        while(xPos < this.canvas.width) {
          context.moveTo(xPos, this.centerY - this.tickSize / 2);
          context.lineTo(xPos, this.centerY + this.tickSize / 2);
          context.stroke();
          context.fillText(unit, xPos, this.centerY + this.tickSize / 2 + 3);
          unit += this.unitsPerTick;
          xPos = Math.round(xPos + xPosIncrement);
        }
        context.restore();
      };

      Graph.prototype.drawYAxis = function() {
        var context = this.context;
        context.save();
        context.beginPath();
        context.moveTo(this.centerX, 0);
        context.lineTo(this.centerX, this.canvas.height);
        context.strokeStyle = this.axisColor;
        context.lineWidth = 2;
        context.stroke();

        // draw tick marks
        var yPosIncrement = this.unitsPerTick * this.unitY;
        var yPos, unit;
        context.font = this.font;
        context.textAlign = 'right';
        context.textBaseline = 'middle';

        // draw top tick marks
        yPos = this.centerY - yPosIncrement;
        unit = this.unitsPerTick;
        while(yPos > 0) {
          context.moveTo(this.centerX - this.tickSize / 2, yPos);
          context.lineTo(this.centerX + this.tickSize / 2, yPos);
          context.stroke();
          context.fillText(unit, this.centerX - this.tickSize / 2 - 3, yPos);
          unit += this.unitsPerTick;
          yPos = Math.round(yPos - yPosIncrement);
        }

        // draw bottom tick marks
        yPos = this.centerY + yPosIncrement;
        unit = -1 * this.unitsPerTick;
        while(yPos < this.canvas.height) {
          context.moveTo(this.centerX - this.tickSize / 2, yPos);
          context.lineTo(this.centerX + this.tickSize / 2, yPos);
          context.stroke();
          context.fillText(unit, this.centerX - this.tickSize / 2 - 3, yPos);
          unit -= this.unitsPerTick;
          yPos = Math.round(yPos + yPosIncrement);
        }
        context.restore();
      };

      Graph.prototype.drawEquation = function(equation, color, thickness) {
        var context = this.context;
        context.save();
        context.save();
        this.transformContext();

        context.beginPath();
        context.moveTo(this.minX, equation(this.minX));

        for(var x = this.minX + this.iteration; x <= this.maxX; x += this.iteration) {
          context.lineTo(x, equation(x));
        }

        context.restore();
        context.lineJoin = 'round';
        context.lineWidth = thickness;
        context.strokeStyle = color;
        context.stroke();
        context.restore();
      };

      Graph.prototype.transformContext = function() {
        var context = this.context;

        // move context to center of canvas
        this.context.translate(this.centerX, this.centerY);

        /*
         * stretch grid to fit the canvas window, and
         * invert the y scale so that that increments
         * as you move upwards
         */
        context.scale(this.scaleX, -this.scaleY);
      };
      var myGraph = new Graph({
        canvasId: 'myCanvas',
        minX: -10,
        minY: -10,
        maxX: 10,
        maxY: 10,
        unitsPerTick: 1
      });

      myGraph.drawEquation(function(x) {
        return 5 * Math.sin(x);
      }, 'green', 3);

      myGraph.drawEquation(function(x) {
        return x * x;
      }, 'blue', 3);

      myGraph.drawEquation(function(x) {
        return 1 * x;
      }, 'red', 3);

    </script>
  </body>
</html>


### 回答1: Matlab 可以通过使用函数 `ode45` 来求解微分方程的解,然后利用函数 `plot` 来绘制解的图像。 具体步骤如下: 1. 定义微分方程:首先,你需要在 Matlab 中定义微分方程,通常使用函数形式表示。 2. 求解微分方程:使用 `ode45` 函数求解微分方程,它需要你指定初始值、积分区间以及微分方程的函数。 3. 绘制相图:使用 `plot` 函数绘制解的图像,可以使用多种不同的图像格式,如曲线图、散点图等。 示例代码如下: ``` % 定义微分方程的函数 function dydt = myode(t,y) dydt = -y; end % 求解微分方程 tspan = [0,5]; y0 = 1; [t,y] = ode45(@myode,tspan,y0); % 绘制相图 plot(t,y) xlabel('t') ylabel('y(t)') title('Solution of the Differential Equation') ``` 以上代码演示了如何求解一个简单的微分方程,并绘制它的相图。 ### 回答2: 在MATLAB中,要画微分方程的相图,可以按照以下步骤操作: 1. 首先,在MATLAB中创建一个新的脚本文件。 2. 在脚本文件中定义微分方程。例如,对于一阶微分方程 dy/dt = f(t, y),可以将其定义为函数 f(t, y)。 3. 在脚本文件中创建一个空的画布,使用`figure`函数。 4. 使用`quiver`函数绘制向量场。向量场是相空间中的矢量箭头,表示微分方程的导数的方向和大小。`quiver`函数的输入参数包括矢量箭头的位置、方向和长度。通常,我们将相空间的范围划分为一定数量的离散点,并计算在这些点上的导数。然后,在相应的位置绘制箭头。这样,相图就显示了不同位置上导数的方向和大小。例如,可以使用以下代码绘制相图: ```MATLAB [x, y] = meshgrid(linspace(x_min, x_max, n), linspace(y_min, y_max, n)); % 创建相空间的位置矩阵,其中n是空间离散点的数量 dx = zeros(size(x)); % 初始化导数矩阵 dy = f(x, y); % 计算在每个位置上的y导数 quiver(x, y, dx, dy); % 绘制相图 ``` 其中,`x_min`、`x_max`、`y_min`和`y_max`是相空间的边界,`f`是微分方程的函数。 5. 使用`hold on`函数使画布保留之前的图像并继续添加新的内容。 6. 使用`plot`函数或其他绘图函数绘制特定的轨迹线。例如,可以使用`ode45`函数求解微分方程,然后使用`plot`函数绘制解的轨迹。 7. 添加坐标轴标签、图标题和图例等修饰功能。 8. 运行脚本文件以生成相图。 希望这个回答能帮助到你! ### 回答3: 要在MATLAB中绘制微分方程的相图,通常需要以下步骤: 1. 首先,需要定义微分方程的函数形式。假设有一个一阶自由变量微分方程dy/dt=f(t,y),其中t是自变量,y是因变量。可以将该微分方程定义为一个名为"diff_eq"的函数,其中输入参数为t和y,返回值为f(t,y)。 2. 接下来,需要确定在相图中要绘制的(t,y)范围。可以选择合适的t和y的范围,以确保相图包含感兴趣的区域。 3. 使用MATLAB的ode45函数,通过数值积分方法来解决微分方程。将"diff_eq"函数作为输入,以及初始值y0,计算t的向量和y的矩阵来表示微分方程的解。 4. 使用MATLAB的quiver函数,利用得到的解向量来绘制相图。quiver函数可以绘制出一系列箭头,箭头的方向和长度代表了微分方程在不同点上的斜率。 下面是示例代码: ``` % Step 1: 定义微分方程的函数形式 function dydt = diff_eq(t, y) dydt = t - y; % Step 2: 确定绘制相图的范围 tspan = [0 10]; yrange = [-10 10]; % Step 3: 使用ode45函数求解微分方程 y0 = 0; % 初始值 [t, y] = ode45(@diff_eq, tspan, y0); % Step 4: 绘制相图 quiver(t, y, ones(size(t)), diff_eq(t, y), 0.5, 'b'); % 绘制箭头,颜色为蓝色 xlim(tspan); ylim(yrange); xlabel('t'); ylabel('y'); title('微分方程的相图'); grid on; % 可以通过调整箭头长度、颜色和其他样式选项来美化相图 ``` 以上是一个简单的绘制微分方程相图的示例。可以根据具体的微分方程和需要进行适当的修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值