Maximum of a non linear ODE (Scilab)

60 Views Asked by At

Let $u'=\frac{2}{t+1.3}sin(u+t+5.78)$ with $u(1)=0.278$. Find the maximum of u(t).

My attempt on SCILAB: I tried to use taylor's method and vary the values ​​of t, but I found an absurdly large value, so I'm not confident, follow my code

function $y=f(t,u)$
$y=(2/(t+1.3))*sin(u+t+5.78)$ endfunction

function $y=ft(t,u)$
$y=(2/(t+1.3))*cos(u+t+5.78)$ endfunction

function $[u]=taylor(a,T,N)$
$t(1)=1$
$u(1)=a$
$h=(T-t(1))/N$
for $n=1:N$
$t(n+1)=t(n)+h$;
$F=f(t(n),u(n))$;
$Ft=ft(t(n),u(n))$;
$u(n+1)=u(n)+h*F+(h^2/2)*Ft$
end
ultimo=u(N+1);
plot(t,u,'r.-');xgrid
endfunction
disp(taylor(0.278,100000,10))\

1

There are 1 best solutions below

0
On

I no longer remember Scilab very well, so I gave this a shot using sympy in python. I'm a python beginner; I'm sure my code isn't the best way to go about this problem. I used Taylor's method to solve the given non-linear ODE.

from sympy import *

t = symbols('t')
u = Function('u')


def taylor_coeffs(n):
    
    d0 = (2*sin(t+u(t)+5.78))/(t+1.3)
    v1 = d0.subs([(t,1),(u(1),0.278)])
    dn = 0
    val = 0
    lst = [0.278,v1]
    
    for i in range(1,n):
        dn = diff(d0,t,1).subs(diff(u(t),t),d0)
        val = dn.subs([(t,1),(u(1),0.278)])
        lst.append(val)
        d0 = dn
    return lst

from math import factorial

def diffsoln(taylor_coeffs,n):
    
    lst = taylor_coeffs(n)
    
    soln = 0
    
    for i in range(0,n):
        soln += lst[i]*((t-1)**i)*(1/factorial(i))
    
    return soln

This part of the code contains two functions. The first one returns a list containing the coefficients (not considering the factorials) of the terms in the Taylor series; the second function takes the list from earlier and returns the actual Taylor series expansion up to $n$ terms.

The function can be called as follows (using n=5):

diffsoln(taylor,5).expand()

The result is:

enter image description here

This is $u(t)$. To find the maxima of u(t):

expr = diffsoln(taylor,5).expand()

solve(diff(expr,t),0)

The result is

[0.529769336163160,
 2.09191959780212 - 1.08854162859148*I,
 2.09191959780212 + 1.08854162859148*I]

These are values of $t$ where $u(t)$ is maximum.

My computer struggles with anything above n = 5; this can be attributed to the fact that my code is inefficient which is no surprise.

I hope this helps. Please do let me know in the comments if this is remotely helpful, and whether my results are correct.