# solving system of ordinary differential equation with ODE solver of matlab

I have a system of equations which describe dynamic nature of a system. There is no closed form solution for this system. So I want to solve it numerically with Matlab solver ODE. But as I'm beginner in this area, I can't write correct code despite of several hours effort. System of equations is as follow:

$$I_c=I_1 + I_2 + I_3$$ $$R_3 = \frac{V_3}{I_3}$$ $$I_1=(C_0+2KV_1)\frac{dV_1}{dt}$$ $$V_1=V_3-I_1 R_1$$ $$I_2=C_2\frac{dV_2}{dt}$$ $$V_2=V_3-I_2 R_2$$ In these equations $I_c, R_1,R_2, R_3, C_0, C_2,$ and $K$ are constants and given. I want to have $V_1, V_2, V_3$ evolution over time t.

You only have derivatives for 2 dependents. The easiest solution would be to express all other dependents in these two, $v_1, V_2$. You have V_3=R_3I_3 \\ \left.\begin{aligned} V_1=V_3-I_1R_1=R_3I_3-R_1I_1 \\ V_2=V_3-I_2R_2=R_3I_3-R_2I_2 \end{aligned}\right\}\implies R_2V_1+R_1V_2=(R_2+R_1)R_3I_3-R_1R_2(I_c-I_3) \\ so given $V_1, V_2$ you can compute $I_3$, then $V_3$ then $I_1, I_2$.

function [I V] = fullstate(V12)
V1 = V12(1);
V2 = V12(2);
I3 = (R2*V1+R1*V2+R1*R2*Ic)/((R1+R2)*R3+R1*R2)
V3 = R3*I3
I1 = (V3-V1)/R1
I2 = (V3-V2)/R2
I = [I1 I2 I3]
V = [V1 V2 V3]
end

function dVdt = V12prime(t,V12)
[ I V ] =  fullstate(V12)
dVdt = [ I(1)/(C0+2*K*V(1)) I(2)/C2 ]
end

V12start = [V1start V2start]
t0 =
tf =
times = t0:dt:tf

[ T V12 ] = ode45(V12prime, times, V12start)


and then, if the matrix dimensions fit (if not, add matrix transpositions to the ODE function)

[ I V ] = fullstate(V12)
plot(T,I(1))