0
$\begingroup$

I get some results z1 from an equation

    s1 = y''[x] + Sin[y[x]] y[x];
    sol1 = NDSolve[{s1 == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 30}];
    sy[x_] = {y[x]} /. s;
    z1 = Table[Take[sy[x]], {x, 5, 30, 5}]
{{{-4.56095}}, {{0.999889}}, {{-4.55976}}, {{0.999555}},{{-4.55738}}, {{0.998999}}}

Now a new equation s2 contains a and b

    s2 = a*y2''[x] + b*Sin[y2[x]] y2[x]

The results z2 should be solved using the same method.

    sol2 = NDSolve[{s2 == 0, y2[0] == 1, y2'[0] == 0}, y2, {x, 0, 30}];
    sy2[x_] = {y2[x]} /. s;
    z2 = Table[Take[sy2[x]], {x, 5, 30, 5}]

Then I want to minimize (z1-z2).(z1-z2) and find a and b

    Nminimize[(z1 - z2).(z1 - z2), {a, b}]

I know my codes can't work, but I'm trying to express thought. I really appreciate your help.

$\endgroup$
2
$\begingroup$

Try (I simplified your code a little bit)

s1 = y''[x] + Sin[y[x]] y[x];
y1 = NDSolveValue[{s1 == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 30}];
(*y1[x] is the solution of ode s1 *)

s2 = a*y2''[x] + b*Sin[y2[x]] y2[x]
Y2 = ParametricNDSolveValue[{s2 == 0, y2[0] == 1, y2'[0] == 0},y2, {x, 0, 30}, {a, b}];
(*Y2[a,b][x] is the parametric(!) solution of ode s2*)

Minimization

NMinimize[Sum[ (y1[x] - Y2[a, b][x])^2 , {x, 5, 30, 5}], {a, b}]
(*{4.06908*10^-13, {a -> 0.980316, b -> 0.986701}}*)    

To get the expected result {a->1,b->1} you should increase the WorkingPrecision inside NDSolve!

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you.But When I try to run a complicated case, I meet another problem, z2 = Table[Take[First[invysol], {Nn + 1, 2 Nn}].u2n[L/2], {t, 5, 20, 1}] Take::normal: Nonatomic expression expected at position 1 in Take[1,{6,10}]. Take::normal: Nonatomic expression expected at position 1 in Take[1,{6,10}]. Take::normal: Nonatomic expression expected at position 1 in Take[1,{6,10}]. General::stop: Further output of Take::normal will be suppressed during this calculation. 'invysol' is the solution of parametric function. and 'z2' is the element I want to take from invysol $\endgroup$ – YzWu Apr 10 at 20:40
  • $\begingroup$ It seems I can't use Take in the parametric function. How to solve this problem? Thank you again @Ulrich Neumann $\endgroup$ – YzWu Apr 10 at 20:41
  • $\begingroup$ Usually Take is applied to list(first argument), I don't understand what you intend to do. $\endgroup$ – Ulrich Neumann Apr 11 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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