How can i run correctly my Backward Difference Formula code ?

7 vues (au cours des 30 derniers jours)
Hazel Can
Hazel Can le 29 Mai 2022
Réponse apportée : VBBV le 30 Mai 2022
%% 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')

Réponses (1)

VBBV
VBBV le 30 Mai 2022
%% 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')

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by