1
$\begingroup$

I want to find out the series of solution of thsol, th0 and b varying Fy from 0 to 10000. But I'm new to mathematica and do not know how to write a for loop with NDSolve. Here is my code:

EE = 114*10^9
h = 0.02
b = 0.1
LL = 1
Fy = 0
MM = 0
Fx = 0
II = h^3*b/12
thsol = NDSolve[{θ''[s]*EE*II == 
    Fx*Sin[θ[s]] - Fy*Cos[θ[s]], θ'[LL] == 
    MM/(EE*II), θ[0] == 0}, θ, {s, 0, LL}]
th0 = (Evaluate[θ[s] /. thsol]) /. s -> LL
b = NIntegrate[Sin[θ[s] /. thsol], {s, 0, LL}]

Thanks a lot!

$\endgroup$

closed as off-topic by C. E., happy fish, MarcoB, Bob Hanlon, Edmund Mar 21 '17 at 0:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – C. E., happy fish, MarcoB, Bob Hanlon, Edmund
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Use the Table command. $\endgroup$ – C. E. Mar 18 '17 at 20:18
2
$\begingroup$

As mentioned by C.E., the simplest solution is to use a Table to wrap your code. (You need to add some CompoundExpression(;), of course. )

lst = Table[
    EE = 114*10^9;
    h = 0.02;
    b = 0.1;
    LL = 1;
    MM = 0;
    Fx = 0;
    II = h^3*b/12;
    thsol = 
     NDSolve[{θ''[s]*EE*II == 
        Fx*Sin[θ[s]] - Fy*Cos[θ[s]], θ'[LL] == 
        MM/(EE*II), θ[0] == 0}, θ, {s, 0, LL}];
    th0 = (Evaluate[θ[s] /. thsol]) /. s -> LL;
    b = NIntegrate[Sin[θ[s] /. thsol], {s, 0, LL}];
    {thsol, th0, b}, {Fy, 0, 10000, 100}]; // AbsoluteTiming
(* {8.664614, Null} *)
lst[[All, 3]] // Flatten // ListPlot

But it's not the most effective way. Using ParametricNDSolveValue and calculate the integration inside it will make the code much faster:

{EE = 114 10^9, h = 0.02, b = 0.1, LL = 1, MM = 0, Fx = 0, II = h^3 b/12};
thsolpara = 
  ParametricNDSolveValue[{θ''[s] EE II == 
     Fx Sin[θ@s] - Fy Cos[θ@s], θ'[LL] == 
     MM/(EE II), θ[0] == 0, intSin'[s] == Sin[θ@s], 
    intSin[0] == 0}, {θ, θ[LL], intSin[LL]}, {s, 0, LL}, Fy];

lst2 = thsolpara /@ Range[0, 10000, 100]; // AbsoluteTiming
(* {3.102206, Null} *)
lst2[[All, 3]] // ListPlot

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thank you so much! They are exactly what I was looking for! $\endgroup$ – Joy Mar 19 '17 at 21:58
  • $\begingroup$ @joy You can click the checkmark sign to accept my answer if you're satisfied with it :) $\endgroup$ – xzczd Mar 20 '17 at 5:46
  • $\begingroup$ Sure, I just did it! It's my first time to post on stackexchange too, thanks for letting me know! $\endgroup$ – Joy Mar 20 '17 at 12:08

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