Search code examples
matlaboctavesympydifferential-equations

In Octave, Is There a Way to Solve an Equation in 2 Variables for 1 of the Variables


I have an MATLAB script that solves an inhomogeneous first-order linear IVP using the Laplace transform. (For this example, the script is set up to solve the IVP enter image description here, enter image description here.)

syms x(t) s X;

a0 = -3;
x0 = 4;
rhs = t^2;

lhs = diff(x,t) + a0*x;
ode = lhs - rhs

Lx = X;
LDx = s*X - x0;
LHS = LDx + a0*Lx;
RHS = laplace(rhs,t,s);
IVP = LHS - RHS;

IVP = collect(IVP,X);

X = solve(IVP, X);
X = partfrac(X);

sol = ilaplace(X, s, t)
check1 = diff(sol,t) - 3*sol
check2 = vpa(subs(sol, t, 0))

If I substitute "factor" for "collect", the script almost works on Octave with the symbolic package linking to SymPy, except for the "solve" command https://www.mathworks.com/help/symbolic/solve.html.

Is there any Octave (or SymPy, if that would function as a workaround) command that will function as a MATLAB symbolic toolbox "solve" command so I can solve the IVP with a Laplace transform with a script so I don't have to solve for X manually, then use "ilaplace"?

Thanks in advance for any assistance you can provide.


Solution

  • OK, so, one of my students solved the problem (I will contact him later in the week to see if he wishes to be publicly acknowledged regarding his solution).

    You just need to define the result of coeff*[1;X] as an equation set equal to 0, say IVPEQ = coeff*[1;X] == 0, then use the symbolic package command solve on this equation, X = solve(IVPEQ, X).

    Here is a version of my previous 1st-order IVP solver with my student's modification

    syms x(t) s X;
    
    a0 = -3;
    x0 = 4;
    rhs = t^2;
    
    lhs = diff(x,t) + a0*x;
    ode = lhs - rhs
    
    Lx = X;
    LDx = s*X - x0;
    LHS = LDx + a0*Lx;
    RHS = laplace(rhs,t,s); % The t and s in laplace aren't necessary, as they are default
    IVP = LHS - RHS;
    
    coeff = coeffs(IVP,X);
    IVPEQ = coeff*[1;X] == 0;
    
    X = solve(IVPEQ,X);
    
    X = partfrac(X);
    
    sol = ilaplace(X, s, t)
    
    Dsol = diff(sol,t);
    check1 = Dsol + a0*sol
    check2 = vpa(subs(sol, t, 0))
    

    and here is the 2nd-order IVP solver with the student's modification

    syms x(t) s X;
    
    a1 =-2;
    a0 = -3;
    x0 = 4;
    xdot0 = 5;
    rhs = t^2;
    
    Dx = diff(x,t);
    D2x = diff(x,t,2);
    lhs = D2x + a1*Dx + a0*x;
    ode = lhs - rhs
    
    Lx = X ;
    LDx = s*X - x0;
    LD2x = s^2*X - x0*s - xdot0;
    LHS = LD2x + a1*LDx + a0*Lx;
    RHS = laplace(rhs,t,s); % The t and s in laplace aren't necessary, as they are default
    IVP = LHS - RHS;
    
    coeff = coeffs(IVP,X);
    IVPEQ = coeff*[1;X] == 0;
    
    X = solve(IVPEQ,X);
    
    X = partfrac(X);
    
    sol = ilaplace(X, s, t)
    
    Dsol = diff(sol,t);
    D2sol = diff(sol,t,2);
    check1 = D2sol + a1*Dsol + a0*sol
    check2 = vpa(subs(sol, t, 0))
    check3 = vpa(subs(Dsol, t, 0))
    

    Thanks again, @Tasos_Papastylianou , for your immense help!