How i implement Backward Difference Formula Method from general code ?
24 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1010865/image.png)
Below is the Backward Difference Formula general code. How can I adapt this code to the above question? Can you please help?
%-------------------------------------------
% Tw0 step Backward Difference Formula Method with fixed point iteration
% [T,Y]=gerifark2(f,tanim,Y1,h); tanim=[t1,tson]
%-------------------------------------------
function [T,Y]=gerifark2(f,tanim,Y1,h)
t1=tanim(1);tson=tanim(2);
eps=0.0001;
t2=t1+h;t3=t2+h;
tanim=[t1,t2];
[T,Y]=geuler(f,tanim,Y1,h);
Y2=Y(2);Y31=Y2;
while t2 < tson
fark=2*eps;
while fark > eps
Y32=4/3*Y2-1/3*Y1+2/3*h*f(t3,Y31);
fark=abs(Y32-Y31);
Y31=Y32;
end
Y3=Y32;Y1=Y2;Y2=Y3;
t1=t2;t2=t3;
T=[T;t3];Y=[Y;Y3];
t3=t3+h;
end
3 个评论
Lateef Adewale Kareem
2022-5-29
编辑:Lateef Adewale Kareem
2022-5-29
its hard to correct your code. you called other function (that I dont have) in your code. if I want to correct, there is no way for me to test. however I can try to solve the problem without using your code at all
I dont think I can help you on this
回答(1 个)
Nipun
2024-4-17
Hi Hazel,
I understand that you want to adapt the given backward difference formula general code for the listed set of equations and constraints.
Upon a scrupulous look at the given code, I can confirm that there are a few functions such as "geuler" which are custom functions and are not elaborated further. Additionally, it is unclear what the function "fn" stands for in the backward difference formula given in the query. The prompt says to derive the twelve calculations - it is unclear as to what calculations are to be performed.
Due to these missing information, it will not be possible to help with adapting the code for a specific case.
However, I can help you with an example code from scratch for this problem. The following code approximates the solution and codes the exact solution. Feel free to make any changes as required.
function bdf2_example()
% Parameters
a_values = [20, -20, -500];
h = 0.01;
t_final = 1;
t = 0:h:t_final;
y_exact = @(t, a) exp(a.*t) + cos(t);
% Loop through each value of a
for a = a_values
% Initial conditions
y = zeros(size(t));
y(1) = 2; % y(0) = 2
y(2) = y_exact(h, a); % Approximate y(1) using the exact solution for simplicity
% Solve using the second-order BDF method
for n = 3:length(t)
f_n = @(y_n) a*(y_n - cos(t(n))) - sin(t(n)); % Define f_n as a function of y_n
% Second-order BDF formula (implicit in y_n)
y(n) = (4*y(n-1) - y(n-2))/3 + (2*h/3)*f_n(y(n-1)); % Approximation, using y(n-1) for simplicity
end
% Plotting
plot(t, y, 'DisplayName', sprintf('BDF2, a = %d', a)); hold on;
end
% Exact solution plot
for a = a_values
plot(t, y_exact(t, a), '--', 'DisplayName', sprintf('Exact, a = %d', a));
end
title('Second-order BDF Solution vs. Exact Solution');
xlabel('t');
ylabel('y(t)');
legend('show');
grid on;
hold off;
end
Hope this helps.
Regards,
Nipun
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!