How can i run correctly my Backward Difference Formula code ?

조회 수: 10 (최근 30일)
Hazel Can
Hazel Can 2022년 5월 29일
답변: VBBV 2022년 5월 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