3
$\begingroup$

I am trying to solve a fourth-order differential equation using the shooting method and I wrote the following code, But this code does not work for me and I don't know what the problem is.

a = 1.3; M = 0.3; S = 1;

shooting[{s1_?NumericQ, s2_?NumericQ}] := 
  Module[{sol, Fsol}, 
   sol = NDSolve[{F''''[
        t] = (a/(a + 1)) S (t F'''[t] + 3 F''[t] + F[t] F''[t] - 
           F[t] F'''[t]) + (M a/(a + 1)) F''[t], F[0] == 0, 
      F'[0] == s1, F''[0] == 0, F'''[0] == s2}, {F}, {t, 0, 1}, 
     Method -> {"Shooting", 
       "StartingInitialConditions" -> {F[0] == 0, F'[0] == s1, 
         F''[0] == 0, F'''[0] == s2}}];
   
   Fsol = F /. sol[[1]];
   Fsol[1]];

shootingResults = FindRoot[shooting[{s1, s2}], {{s1, 0}, {s2, 0}}];

s1Final = s1 /. shootingResults
s2Final = s2 /. shootingResults

h = 0.01; n = 100; grid = Range[0, 1, h]; coeff = (a/(a + 1));

eqns = {F''''[
     t] = (a/(a + 1)) S (t F'''[t] + 3 F''[t] + F[t] F''[t] - 
        F[t] F'''[t]) + (M a/(a + 1)) F''[t]};

bcs = {F[0] == 0, F'[0] == s1Final, F''[0] == 0, F'''[0] == s2Final};

sol = NDSolve[{eqns, bcs}, {F}, {t, 0, 1}, 
   Method -> {"MethodOfLines", 
     "SpatialDiscretization" -> {"FiniteDifference", 
       "DifferenceOrder" -> 2, "Coordinates" -> {grid}}}];

Plot[Evaluate[F[t] /. sol[[1]]], {t, 0, 1}, PlotLegends -> "F[t]"]
$\endgroup$
2
  • $\begingroup$ Let's start by looking at the error message: NDSolve: Equation or list of equations expected instead of ... Now look at the first equation in your NDSolve, it uses = (Set) instead of == (Equal). Use Quit to clear all variables, then change that = to ==. Second, you cannot use FindRoot for two variables. $\endgroup$
    – Domen
    Commented Oct 1, 2023 at 17:22
  • $\begingroup$ Because all the boundary conditions are at t = 0, this is an initial value problem, but the Shooting method is for boundary value problems. If, in fact, you actually are trying to solve a boundary value problem, you need to include some boundary conditions at t = 1 in NDSolve. $\endgroup$
    – bbgodfrey
    Commented Oct 2, 2023 at 0:25

1 Answer 1

0
$\begingroup$

I see that you're trying to solve a fourth-order differential equation using the shooting method in Mathematica. I've reviewed your code, and it looks mostly correct. However, there's one issue in your code. In your shooting function, you're trying to extract s1 and s2 from the shootingResults using s1Final = s1 /. shootingResults and s2Final = s2 /. shootingResults. However, you should use s1Final = s1 /. shootingResults[[1]] and s2Final = s2 /. shootingResults[[2]] to access the solutions properly.

Here's the corrected code:

a = 1.3; M = 0.3; S = 1;

shooting[{s1_?NumericQ, s2_?NumericQ}] := 
  Module[{sol, Fsol}, 
   sol = NDSolve[{F''''[
        t] == (a/(a + 1)) S (t F'''[t] + 3 F''[t] + F[t] F''[t] - 
           F[t] F'''[t]) + (M a/(a + 1)) F''[t], F[0] == 0, 
      F'[0] == s1, F''[0] == 0, F'''[0] == s2}, {F}, {t, 0, 1}, 
     Method -> {"Shooting", 
       "StartingInitialConditions" -> {F[0] == 0, F'[0] == s1, 
         F''[0] == 0, F'''[0] == s2}}];
   
   Fsol = F /. sol[[1]];
   Fsol[1]];

shootingResults = FindRoot[shooting[{s1, s2}], {{s1, 0}, {s2, 0}}];

s1Final = s1 /. shootingResults[[1]];
s2Final = s2 /. shootingResults[[2]];

h = 0.01; n = 100; grid = Range[0, 1, h]; coeff = (a/(a + 1));

eqns = {F''''[
     t] == (a/(a + 1)) S (t F'''[t] + 3 F''[t] + F[t] F''[t] - 
        F[t] F'''[t]) + (M a/(a + 1)) F''[t]};

bcs = {F[0] == 0, F'[0] == s1Final, F''[0] == 0, F'''[0] == s2Final};

sol = NDSolve[{eqns, bcs}, {F}, {t, 0, 1}, 
   Method -> {"MethodOfLines", 
     "SpatialDiscretization" -> {"FiniteDifference", 
       "DifferenceOrder" -> 2, "Coordinates" -> {grid}}}];

Plot[Evaluate[F[t] /. sol[[1]]], {t, 0, 1}, PlotLegends -> "F[t]"]
$\endgroup$
3
  • $\begingroup$ The code gave the following errors: FindRoot::nveq: The number of equations does not match the number of variables in FindRoot[shooting[{s1,s2}],{{s1,0},{s2,0}}]. $\endgroup$
    – ahmed
    Commented Oct 2, 2023 at 6:55
  • $\begingroup$ ReplaceAll::reps: {shooting[{s1,s2}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. $\endgroup$
    – ahmed
    Commented Oct 2, 2023 at 6:57
  • 5
    $\begingroup$ Is this an AI-generated answer? If it is, please know that it is strongly advised against posting such answers. $\endgroup$
    – Domen
    Commented Oct 2, 2023 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.