我應該寫一個MATLAB函數,它以y'(t)= a * y(t)+ b形式的一階常微分方程爲初始點y(t0)= y0作爲輸入並計算前15個點解決方案。還繪製了前15個點的解答曲線。 而我們想要求解的方程是y'(t)= 4 * y(t)+1,初始點y(0)= 0。寫一個實現歐拉方法的matlab函數?
對於這個函數我寫了下面的代碼,但是這給了我一個關於y的錯誤。我應該如何正確實施歐拉功能?而且我也不能確定我怎麼能得出解決方案的曲線..
function E=euler(f,y)
%Input - f is the function entered as a string 'f'
% - a and b are the left and right endpoints
% - ya is the initial condition y(a)
% - M is the number of steps
%Output - E=[T' Y'] where T is the vector of abscissas and
% Y is the vector of ordinates
h=0.1;
y(0)=0;
for j=0:15
Y(j+1)=Y(j)+h*feval(4*(y(t)+1));
end
請參閱此問題的答案... http://stackoverflow.com/questions/13063060/implementing-explicit-euler-method-for-odes-in-matlab – ccook