2012-12-16 31 views
0

我應該寫一個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 
+0

請參閱此問題的答案... http://stackoverflow.com/questions/13063060/implementing-explicit-euler-method-for-odes-in-matlab – ccook

回答

1

補丁:

h = 0.1; 
y(1) = 0; 
for j = 1:16 
    Y(j + 1) = Y(j) + h * feval(4 * (y(t - 1) + 1)); 
end 

好了,我不知道有關的數學部分,但 - 該指數需要以啓動「1」。其他然後例如在C中,您不能使用「0」作爲索引。

+0

哦,我看到你開始for循環與2,但你不在代碼中初始化y(0)爲0,這會給出任何錯誤? –

+0

仔細閱讀,我初始化「y(1)= 0;」 ...我基本上給所有指數加了「1」。 –

+0

好吧,一個小的更正,使其更容易看到。此外,「t」沒有定義。它從何而來? –