How can i run correctly my Backward Difference Formula code ?

14 ビュー (過去 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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by