How can i run correctly my Backward Difference Formula code ?

8 次查看(过去 30 天)
%% Backward Difference Formula Method %%
clc; clear all;
h=0.01;
t=0:h:1;
n=numel(t);
mu = 20;
f_m = @(t,y) mu*(y-cos(t))-sin(t);
exact = @(t) exp(mu*t)+cos(t);
%initials%
y_m(1)=exact(0);
y_m(2)=exact(h);
%Adam-Bashforth method%
x = ( 4*y_m(n-1)-y_m(n-2) )/3 + 2*h/3* ( mu*( x - cos(t(n)) ) - sin(t(n)) )
S=solve(x)
for i=3:n
y_m(i)=(4.*y_m(i-1)-y_m(i-2))/3+(2/3).*h*(3*f_m(t(i)))
end
plot(t, exact(t));
hold
plot(t,y);
%plot(t,y,'-o');
legend('Exact Solution','BDF Solution')
xlabel('t')
ylabel('y')
title('When h = 0.01 and µ=20')

回答(1 个)

VBBV
VBBV 2022-5-30
%% Backward Difference Formula Method %%
clc; clear all;
h=0.01;
t=0:h:1;
n=numel(t);
mu = [20 -20]
mu = 1×2
20 -20
hold all
for k = 1:length(mu)
f_m = @(t,y) mu(k)*(y-cos(t))-sin(t);
exact = @(t) exp(mu(k)*t)+cos(t);
%initials%
y_m(1)=exact(0);
y_m(2)=exact(h);
%Adam-Bashforth method%
syms y
y = ( 4*y_m(1)-y_m(2) )/3 + 2*h/3* ( mu(k)*( y - cos(t(n)) ) - sin(t(n)) );
S=solve(y,[0]);
Y_m = [y_m zeros(length(t)-2,1).'];
for i=3:n
Y_m(i)=(4.*y_m(1)-y_m(2))/3+(2/3).*h*(3*f_m(t(i),Y_m(i-1)));
end
subplot(211)
plot(t, exact(t));
subplot(212)
plot(t,Y_m);
hold on
end
xlabel('t')
ylabel('y')
legend('\mu = 20','\mu = -20','location','best')

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by