回覆列表
  • 1 # madgo1147

    %尤拉法解一階常微分方程%例子dy/h=-y+x+1%f=inline("-y+x+1","x","y"); %微分方程的右邊項f = inline("x-2*y","x","y");y0 = 2; %初始條件h = 0.025; %步長xleft = 0; %區域的左邊界xright = 1; %區域的右邊界x = xleft:h:xright;n = length(x); %前向尤拉法y = y0;for i=2:n y(i)=y(i-1)+h*f(x(i-1),y(i-1)); endplot(x,y,"ro");hold on;%改進尤拉法y = y0;for i=2:n y(i)=y(i-1)+h/2*( f(x(i-1),y(i-1))+f(x(i),y(i-1))+h*(f(x(i-1),x(i-1)))); endplot(x,y,"g+");%精確解用作圖xx = x;f = dsolve("Dy=x-2*y","y(0)=2","x");%求出解析解y = subs(f,xx); %將xx代入解析解,得到解析解對應的數值plot(xx,y);legend("前向尤拉法","改進尤拉法","解析解");

  • 中秋節和大豐收的關聯?
  • hive中如何呼叫python函式?