# Solving system of equations depending on if condition

1 view (last 30 days)
Jake Barlow on 31 Dec 2021
Commented: Jake Barlow on 31 Dec 2021
Hi there,
I am trying to solve eq1 and eq2 below and plot the results for U(t) and V(t) depending on the initial conditions. The initial conditions are
y0 = [V(0) dV(0) U(0) dU(0)], and if V(0)^2+U(0)^2=0, I want to solve another system of equations instead. However,
when running the code below, I get a blank plot, so it seems that the if condition is not implement correctly. Could you please help?
syms U(t) V(t)
dU=diff(U,t);
dV=diff(V,t);
%Initial Conditions
y0 = [0 -1 0 -1];
if U^2 + V^2 == 0
eq1 = diff(U, t, 2) == (1-(sqrt(U^2+V^2)^2)/6+(sqrt(U^2+V^2))^4/120);
eq2 = diff(V, t, 2) == (1-(sqrt(U^2+V^2)^2)/6+(sqrt(U^2+V^2))^4/120);
else
eq1 = diff(U, t, 2) == cos(sqrt(U^2+V^2))/sqrt(U^2+V^2);
eq2 = diff(V, t, 2) == cos(sqrt(U^2+V^2))/sqrt(U^2+V^2);
end
[OTV,Subs] = odeToVectorField([eq1,eq2])
M = matlabFunction(OTV,'vars', {'t','Y'});
t1 = 5;
interval = [0 5]; %time interval
tValues = linspace(interval(1),interval(2),1000);
[t,y] = ode45(M,tValues,y0);
yValues1 = y(:,3); %U(t) solution
yValues2 = y(:,1); %V(t) solution
figure
plot(yValues1,yValues2, 'Color', 'r')
xlabel('\$U(t)\$', 'Interpreter','latex')
ylabel('\$V(t)\$', 'Interpreter','latex')
##### 2 CommentsShowHide 1 older comment
Jake Barlow on 31 Dec 2021
Hi Torsten, thank you very much for your comment. Much appreciated.

R2021b

### Community Treasure Hunt

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

Start Hunting!