{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "9f9c1606", "metadata": {}, "outputs": [], "source": [ "from system_eqns import systemEqns\n", "from sympy import Function, Derivative, symbols, Matrix, Eq, simplify, sin, cos" ] }, { "cell_type": "code", "execution_count": 2, "id": "6d83b5a0", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d t} \\operatorname{Vc_{1}}{\\left(t \\right)} = - \\frac{\\operatorname{Vc_{2}}{\\left(t \\right)}}{C R}$" ], "text/plain": [ "Eq(Derivative(Vc1(t), t), -Vc2(t)/(C*R))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d t} \\operatorname{Vc_{2}}{\\left(t \\right)} = \\frac{- V_{in} + \\operatorname{Vc_{1}}{\\left(t \\right)} - \\operatorname{Vc_{2}}{\\left(t \\right)}}{C R}$" ], "text/plain": [ "Eq(Derivative(Vc2(t), t), (-V_{in} + Vc1(t) - Vc2(t))/(C*R))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[(a_2*omega, -a_1*omega), (a_3/(C*R), a_4/(C*R))]\n", "[(a_4*omega, -a_3*omega), (-a_1/(C*R), -a_2/(C*R)), (a_5/(C*R), a_6/(C*R)), (a_3/(C*R), a_4/(C*R))]\n", "[a_1, a_2, a_3, a_4, a_5, a_6]\n", "a_2*omega -a_1*omega\n", "a_3/(C*R) a_4/(C*R)\n", "a_4*omega -a_3*omega\n", "-a_1/(C*R) -a_2/(C*R)\n", "a_5/(C*R) a_6/(C*R)\n", "a_3/(C*R) a_4/(C*R)\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\omega & 0 & 0 & \\frac{1}{C R} & 0 & 0\\\\0 & \\omega & \\frac{1}{C R} & 0 & 0 & 0\\\\0 & - \\frac{1}{C R} & - \\omega & \\frac{1}{C R} & 0 & \\frac{1}{C R}\\\\- \\frac{1}{C R} & 0 & \\frac{1}{C R} & \\omega & \\frac{1}{C R} & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ -omega, 0, 0, 1/(C*R), 0, 0],\n", "[ 0, omega, 1/(C*R), 0, 0, 0],\n", "[ 0, -1/(C*R), -omega, 1/(C*R), 0, 1/(C*R)],\n", "[-1/(C*R), 0, 1/(C*R), omega, 1/(C*R), 0]])" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[a_1, a_2, a_3, a_4, a_5, a_6]\n" ] } ], "source": [ "A_0, A_1, A_2, B_1, B_2, R1, R2, R3, Vin, C1, C2 = symbols(\"A_0 A_1 A_2 B_1 B_2 R_1 R_2 R_3 V_{in} C_1 C_2\")\n", "Vc1 = Function('Vc1')\n", "Vc2 = Function('Vc2')\n", "Vc3 = Function('Vc3')\n", "R1,R2, Vin, Vc1, Vc2, C1,C2\n", "Vin, Vc, C, R, i, t, f, A, tau, w = symbols(\"V_{in} V_c C R i t f A tau omega\")\n", "\n", "m = Matrix([[R1, -R1], [-R1, R1-R2]])\n", "v = Matrix([Vin-Vc1(t), Vc2(t)])\n", "mtmp = m.inv()*v\n", "\n", "\n", "eqn_1 = Eq(C1 * Vc1(t).diff()/C, mtmp[0]/C)\n", "eqn1 = eqn_1.subs([(R1, R), (R2, R), (C1,C)])\n", "\n", "eqn_2 = Eq(Vc2(t).diff(), simplify(mtmp[1]/C))\n", "eqn2 = eqn_2.subs([(R1, R), (R2, R)])\n", "\n", "display(eqn1)\n", "display(eqn2)\n", "\n", "sysEqns = systemEqns()\n", "\n", "res1 = sysEqns.extractCoeffsFromDiffEqn(eqn1)\n", "print (res1)\n", "res2 = sysEqns.extractCoeffsFromDiffEqn(eqn2)\n", "print (res2)\n", "print (sysEqns.symbols_used)\n", "\n", "M1, syms_used = sysEqns.orderedMatrix([res1, res2])\n", "display(M1)\n", "print (syms_used)" ] }, { "cell_type": "code", "execution_count": 3, "id": "09a4e294", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d t} \\operatorname{Vc_{1}}{\\left(t \\right)} = - \\frac{\\operatorname{Vc_{2}}{\\left(t \\right)}}{C R}$" ], "text/plain": [ "Eq(Derivative(Vc1(t), t), -Vc2(t)/(C*R))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eqn1" ] }, { "cell_type": "code", "execution_count": 4, "id": "1a0e52b2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eq(Derivative(Vc1(t), t), -Vc2(t)/(C*R))\n" ] } ], "source": [ "print(eqn1)" ] }, { "cell_type": "code", "execution_count": 5, "id": "96a64ff7", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\omega & 0 & 0 & \\frac{1}{C R}\\\\0 & \\omega & \\frac{1}{C R} & 0\\\\0 & - \\frac{1}{C R} & - \\omega & \\frac{1}{C R}\\\\- \\frac{1}{C R} & 0 & \\frac{1}{C R} & \\omega\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ -omega, 0, 0, 1/(C*R)],\n", "[ 0, omega, 1/(C*R), 0],\n", "[ 0, -1/(C*R), -omega, 1/(C*R)],\n", "[-1/(C*R), 0, 1/(C*R), omega]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqmat = M1[:4,:4]\n", "sqmat" ] }, { "cell_type": "code", "execution_count": 6, "id": "4c6168d6", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- C R \\omega & 0 & 0 & 1\\\\0 & C R \\omega & 1 & 0\\\\0 & -1 & - C R \\omega & 1\\\\-1 & 0 & 1 & C R \\omega\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[-C*R*omega, 0, 0, 1],\n", "[ 0, C*R*omega, 1, 0],\n", "[ 0, -1, -C*R*omega, 1],\n", "[ -1, 0, 1, C*R*omega]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqmat *C*R\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "94d52194", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\frac{C^{4} R^{4} \\omega^{3}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C R}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{2} R^{2} \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{3} R^{3} \\omega^{2} - C R}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1}\\\\\\frac{C R}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{4} R^{4} \\omega^{3}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{3} R^{3} \\omega^{2} - C R}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & - \\frac{C^{2} R^{2} \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1}\\\\- \\frac{C^{2} R^{2} \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{- C^{3} R^{3} \\omega^{2} + C R}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{- C^{4} R^{4} \\omega^{3} + C^{2} R^{2} \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{3} R^{3} \\omega^{2}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1}\\\\\\frac{- C^{3} R^{3} \\omega^{2} + C R}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{2} R^{2} \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{3} R^{3} \\omega^{2}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} & \\frac{C^{4} R^{4} \\omega^{3} - C^{2} R^{2} \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ -C**4*R**4*omega**3/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), C*R/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), C**2*R**2*omega/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), (C**3*R**3*omega**2 - C*R)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1)],\n", "[ C*R/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), C**4*R**4*omega**3/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), (C**3*R**3*omega**2 - C*R)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), -C**2*R**2*omega/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1)],\n", "[ -C**2*R**2*omega/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), (-C**3*R**3*omega**2 + C*R)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), (-C**4*R**4*omega**3 + C**2*R**2*omega)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), C**3*R**3*omega**2/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1)],\n", "[(-C**3*R**3*omega**2 + C*R)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), C**2*R**2*omega/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), C**3*R**3*omega**2/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1), (C**4*R**4*omega**3 - C**2*R**2*omega)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1)]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqmat**-1" ] }, { "cell_type": "code", "execution_count": 8, "id": "b6558bc4", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0\\\\0\\\\- \\frac{A}{C R}\\\\0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0],\n", "[ 0],\n", "[-A/(C*R)],\n", "[ 0]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = Matrix([0,0, -A/(C*R), 0]);v" ] }, { "cell_type": "code", "execution_count": 9, "id": "8fe7a8d6", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\frac{A C R \\omega}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1}\\\\- \\frac{A \\left(C^{3} R^{3} \\omega^{2} - C R\\right)}{C R \\left(C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1\\right)}\\\\- \\frac{A \\left(- C^{4} R^{4} \\omega^{3} + C^{2} R^{2} \\omega\\right)}{C R \\left(C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1\\right)}\\\\- \\frac{A C^{2} R^{2} \\omega^{2}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ -A*C*R*omega/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1)],\n", "[ -A*(C**3*R**3*omega**2 - C*R)/(C*R*(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1))],\n", "[-A*(-C**4*R**4*omega**3 + C**2*R**2*omega)/(C*R*(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1))],\n", "[ -A*C**2*R**2*omega**2/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1)]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coeffs = (sqmat**-1)*v; coeffs" ] }, { "cell_type": "markdown", "id": "ea7339f8", "metadata": {}, "source": [ "Lets try subsituting in the original values, for a1, a2, a3, a4 and see what happens" ] }, { "cell_type": "code", "execution_count": 10, "id": "ce122389", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\frac{A C R \\omega \\cos{\\left(\\omega t \\right)}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} - \\frac{A \\left(C^{3} R^{3} \\omega^{2} - C R\\right) \\sin{\\left(\\omega t \\right)}}{C R \\left(C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1\\right)}$" ], "text/plain": [ "-A*C*R*omega*cos(omega*t)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1) - A*(C**3*R**3*omega**2 - C*R)*sin(omega*t)/(C*R*(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1))" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vc1 = coeffs[1]*sin(sysEqns.w * t) + coeffs[0]*cos(sysEqns.w*t)\n", "vc1" ] }, { "cell_type": "code", "execution_count": 11, "id": "47108734", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\frac{A C^{2} R^{2} \\omega^{2} \\sin{\\left(\\omega t \\right)}}{C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1} - \\frac{A \\left(- C^{4} R^{4} \\omega^{3} + C^{2} R^{2} \\omega\\right) \\cos{\\left(\\omega t \\right)}}{C R \\left(C^{4} R^{4} \\omega^{4} - C^{2} R^{2} \\omega^{2} + 1\\right)}$" ], "text/plain": [ "-A*C**2*R**2*omega**2*sin(omega*t)/(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1) - A*(-C**4*R**4*omega**3 + C**2*R**2*omega)*cos(omega*t)/(C*R*(C**4*R**4*omega**4 - C**2*R**2*omega**2 + 1))" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vc2 = coeffs[3]*sin(sysEqns.w * t) + coeffs[2]*cos(sysEqns.w*t)\n", "vc2" ] }, { "cell_type": "code", "execution_count": 12, "id": "c9d136fe", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 0$" ], "text/plain": [ "0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The following shoulod simplify to zero as a self check our calculations are correct \n", "simplify(vc1.diff(t) + vc2/(C*R))" ] }, { "cell_type": "code", "execution_count": 13, "id": "55f9c8c8", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle A \\sin{\\left(\\omega t \\right)}$" ], "text/plain": [ "A*sin(omega*t)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vc3 = A*sin(sysEqns.w*t); vc3" ] }, { "cell_type": "code", "execution_count": 14, "id": "91f1dec6", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 0$" ], "text/plain": [ "0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify((vc3-vc1+vc2)/(R*C)+vc2.diff(t))" ] }, { "cell_type": "code", "execution_count": 15, "id": "74a52129", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d t} \\operatorname{Vc_{1}}{\\left(t \\right)} = \\frac{A \\sin{\\left(\\omega t \\right)} - \\operatorname{Vc_{1}}{\\left(t \\right)} - \\operatorname{Vc_{3}}{\\left(t \\right)}}{C R}$" ], "text/plain": [ "Eq(Derivative(Vc1(t), t), (A*sin(omega*t) - Vc1(t) - Vc3(t))/(C*R))" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eqn1 = Eq(Derivative(Vc1(t), t), (A*sin(w*t) - Vc1(t) - Vc3(t))/(C*R))\n", "eqn1" ] }, { "cell_type": "code", "execution_count": 16, "id": "a437d3c6", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\omega & 0 & 0 & \\frac{1}{C R} & 0 & 0\\\\0 & \\omega & \\frac{1}{C R} & 0 & 0 & 0\\\\0 & - \\frac{1}{C R} & - \\omega & \\frac{1}{C R} & 0 & \\frac{1}{C R}\\\\- \\frac{1}{C R} & 0 & \\frac{1}{C R} & \\omega & \\frac{1}{C R} & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ -omega, 0, 0, 1/(C*R), 0, 0],\n", "[ 0, omega, 1/(C*R), 0, 0, 0],\n", "[ 0, -1/(C*R), -omega, 1/(C*R), 0, 1/(C*R)],\n", "[-1/(C*R), 0, 1/(C*R), omega, 1/(C*R), 0]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M1" ] }, { "cell_type": "code", "execution_count": 17, "id": "3233946f", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{C R}$" ], "text/plain": [ "1/(C*R)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M1[2,3]" ] }, { "cell_type": "code", "execution_count": 18, "id": "09d730de", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\omega & 0 & 0 & \\frac{1}{C R} & 0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([[-omega, 0, 0, 1/(C*R), 0, 0]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M1.row(0)" ] }, { "cell_type": "code", "execution_count": 19, "id": "f461c002", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-omega, 0, 0, 1/(C*R), 0, 0]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(M1.row(0))" ] }, { "cell_type": "code", "execution_count": null, "id": "d08bf393", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }