Write a MATLAB program to solve the following two-point boundary value problems and then compare numerical solutions graphically. (a) d 2 θ dt2 = −θ(t), 0 < t < 2π, θ(0) = 0.7, θ(2π) = 0.7. 2 (b) d 2 θ dt2 = − sin(θ(t)), 0 < t < 2π, θ(0) = 0.7, θ(2π) = 0.7.
% a.
% defining eqn
bvpfcn = @(t,Theta) [Theta(2) -Theta(1)];
% coding boundary condition
bcfcn = @(Ta,Tb) [Ta(1)-0.7 Tb(1)-0.7];
% defining guess function
guess = @(t) [sin(t) cos(t)];
% defing tmesh
tmesh = linspace(0,2*pi,20);
% defining solution
solinit = bvpinit(tmesh, guess);
% solve eqn
sol = bvp4c(bvpfcn, bcfcn, solinit);
% plot sulution
figure;
plot(sol.x, sol.y, '-o');
% label
xlabel("t");
ylabel("Theta");
title("a. t vs Theta");
legend("Theta","Theta'");
% b.
% defining eqn
bvpfcn = @(t,Theta) [Theta(2) -sin(Theta(1))];
% coding boundary condition
bcfcn = @(Ta,Tb) [Ta(1)-0.7 Tb(1)-0.7];
% defining guess function
guess = @(t) [sin(t) cos(t)];
% defing tmesh
tmesh = linspace(0,2*pi,20);
% defining solution
solinit = bvpinit(tmesh, guess);
% solve eqn
sol = bvp4c(bvpfcn, bcfcn, solinit);
% plot sulution
figure;
plot(sol.x, sol.y, '-o');
% label
xlabel("t");
ylabel("Theta");
title("b. t vs Theta");
legend("Theta","Theta'");
Comments
Leave a comment