在MATLAB中实现显式Euler方法(用于ODE) [英] Implementing explicit Euler method (for ODEs) in MATLAB

查看:1102
本文介绍了在MATLAB中实现显式Euler方法(用于ODE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索,但找不到任何东西.

I've searched everywhere, and I can't find anything.

首先,我只想说我从未使用过Mat Lab,所以我不知道自己在做什么.

First of all, let me just say I have never used Mat Lab so I have no idea what I am doing what so ever.

我尝试了一些尝试,但是没有任何效果.显然y(0)= 2试图创建一个值为2的0单位列表?

I have tried a few things, but none have worked. Apparently y(0)=2 tries to create a list of 0 units with the value of 2?

无论如何,有人可以帮助我吗? 我需要在Mat Lab中编写一个灵活的Euler方法方程来解决一些这样的方程.

Anyway, can someone help me? I need to write a flexible Euler's Method equation in Mat Lab to solve a few equations like this.

1)y'= 5-3sqrt(y); y(0)= 2

1) y' = 5-3sqrt(y) ; y(0)=2

h = .1,.05,.025,.01&& t = .5,1,1.5,2,2.5,3

With h= .1 , .05 , .025, .01 && t =.5 ,1 , 1.5 , 2 , 2.5 ,3

我不知道自己在做什么.

I have no idea what I am doing.

推荐答案

让我们假设以恒定时间步长(例如dt>0)实现该方法. 如果要在时间T>0之前对方程进行积分,则考虑时间离散化

Let us suppose to implement the method with constant time step, say dt>0. If we wanna integrate the equation up to a time T>0, we consider a time discretization

tt = 0:dt:T;

出于速度目的,我们最好预先分配解决方案向量,

We'd better pre-allocate our solution vector for speed purpose, i.e.

yy=zeros(1,length(tt));

yy将包含我们将生成的解决方案的时间上的第一阶近似值(即,几乎不使用符号

yy will contain the first order-in-time approximation of the solution we will produce (i.e., with little abuse of notation,

yy(1)==y_r(t=0)

yy(end)==y_r(t=T) + global error,

其中函数y_r=y_r(t)是我们的真正解决方案).

where the function y_r=y_r(t) is our real solution).

假设我们有一个正常形式的一阶ODE,即

Supposedly, we have a first order ODE in normal form, i.e.

 dy_r / dt = f(y_r;t)

和初始基准

 y_r(t=0)=y_0

(即我们遇到了柯西问题).因此,我们应该首先初始化解决方案向量

(i.e. we have a Cauchy problem). Thus, we should firstly initialize our solution vector

  yy(1) = y_0;

然后,我们可以找到将来的解决方案,即

then, we can find the solution for future times, i.e.

  N = length(tt);
  for t = 2 : N        // we should look at future times, thus we start from 2
                       // NOTE: this is first order explicit Euler scheme.
       yy(t) = yy(t-1) + dt*f(yy(t-1),t);
  end

我们完成了.现在我们可以绘制解决方案了.

We're done. We can now plot the solution.

  plot(tt,yy);


现在的重点是:您是否对及时的准确性满意?

认为如果您使用此方案来解决例如(哈密顿量)问题(例如简单的谐波振荡器),它将为您的系统提供人工激励(正确地,您会看到偏离正确的哈密顿量轨道).简而言之,您的解决方案是完全人为的解决方案.

Think that if you use this scheme to solve e.g. Hamiltonian problems (say the simple harmonic oscillator), it will give artificial excitation to your system (properly, you can see a drift out of your correct Hamiltonian orbit). In few words, after little time your solution is completely artificial.

实际上,当您解决实际问题时,必须仔细考虑您的问题和物理学,然后选择合适的数值方案来求解方程.很快,您可能会被要求实施更精确的方案,例如 Runge Kutta (您可以更好地信任它,但至少要保留一点,至少以其原始形式).

Indeed, when you solve real problems, you have to carefully consider your problem and your physics, and then choose a proper numerical scheme to solve your equation. Soon, probably you will be asked to implement more accurate schemes such as Runge Kutta (which you can better trust, but just a little bit, at least in their original form).

这篇关于在MATLAB中实现显式Euler方法(用于ODE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆