problem:
用 MATLAB 写一个程序求 f'(0) for f(x) = e^x and
h = 1; 0:1; 0:01 0:001; 0:0001 using the forward dierence formula(前向差分 )
and the central dierence formula (中心差分法 ). 最大的 h直是多少,根据你的matlab程序计算 according
to your computations?
我自己大概写了一下的code,哪位高手帮忙看看,跪求了。。
Problem 1.
%creat user defined function file: euler forward.m
(1) Define function f(x,y)
function [x,y]=euler_forward(f,xinit,yinit,xfinal,n)
% Euler approximation for ODE initial value problem
% Euler forward method
(2)input initial values x0 and y0
% Initialization of x and y as column vectors
x=[xinit zeros(2,n)]; y=[yinit zeros(2,n)];
(3) input step sizes h and number of steps n
% Calculation of h from xinit, xfinal, and n
h=(xfinal-xinit)/n;
(4) Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x(i),y(i));
end
(5) output x and y.
(6) calculation of exact values.
ode45(@xdot,tspan,y0)
%built in functions
f=@(x,y) x./y;
% Calculate exact solution for h=0.01
g=@(x) (e^x);
x[0,100]
xe=[0:0.01:1];
ye=g(xe);
% Calculate exact solution for h=0.1
g=@(x) (e^x);
x[0,100]
xe=[0:0.1:1];
ye=g(xe);
%calculate the exact solution for h=0.001
g=@(x) (e^x);
x[0,100]
xe=[0:0.001: 1];
ye=g(xe);
%calculate the exact solution for h=0.0001
g=@(x) (e^x);
x[0,100]
xe=[0:0.0001: 1];
ye=g(xe);
% Call function
[x4,y4]=ode45(f,[0,1],1);
(8) Estimate errors
error1=['Forward error: ' num2str(-100*(ye(end)-y1(end))/ye(end)) '%'];
==============================================================


雷达卡




京公网安备 11010802022788号







