这是一个工作程序,它在(a,b)和(a,b)之间进行欧拉法(对于diff eq),步长为h,起始值为y0。
这里的函数是相当初级的,希望能给你一个起点!
function yf = euler(a,b,h,y0)
%% This Program implements Euler's Method
% The user must input their function in the form given in the print
% statement.
%% Variables:
% a = start point
% b = end point
% h = stepsize
% y0 = initial value of y
%% Implementation
uf = input('enter your function in terms of t and yt in single quotes: \n');
%Taking in uf, as string or else INLINE will fail
f = inline(uf); %turn the string UF into a function in variable y,t
% Keep the values for plotting
%% Step 1
% Assign initial values
N = (b-a)/h;
y = zeros(N,1);
y(1) = y0;
t(1)=a;
%% Step 2
% Step through Euler's Method, reassign values, and plot.
for i = 2: N
y(i) = y(i-1) + h*f(y(i-1)); %Each approximation
t(i) = a + i*h;
yf = y(i);
end
plot(t,y,'b+');
data = [ t' y]; % Turn Y into a percent, and save as columns to write to Excel
xlswrite('Euler_Data.xls',data,1,'A3');
end