How can i run correctly my Backward Difference Formula code ?

3 Ansichten (letzte 30 Tage)
Hazel Can
Hazel Can am 29 Mai 2022
Beantwortet: VBBV am 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')

Antworten (1)

VBBV
VBBV am 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')

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by