nm

8552 Reputation

19 Badges

12 years, 354 days

MaplePrimes Activity


These are questions asked by nm

I have a list of objects. Each object contains many fields, one of these fields happened to be a solution to an ode.

At the end of solving the ODE, I'd like to remove all objects which contain the same solution. 

I can not do the normal trick in Maple to remove duplicates from a list, which is to convert the list to a set and let Maple automatically remove duplicates because in this case each entry in the list is an object of many fields and hence each object is always different. 

I only want to remove the objects from the list which has the same specific field.

Currently what I do is the following:   Do a first scan, and loop over all entries in the list to find the duplicates. Each time I find a duplicate solution, mark a field in the object as it is being duplicate.

Then do another scan at the end, to build a new list, which only adds those objects not marked as duplicates.

It works, but was wondering if Maple or someone might have better suggestion or more elegent way to do this.

In this MWE I am using a list and adding objects to it making it grow dynamically. I know it would be better to use Array, but for now I'd like to stick to a list, since the number of solutions is normally small. May be in the future I will change the list to Array to avoid building a list dynamically.

This example code builds a list of 5 objects

restart;
ode_solution_type:=module()
    option object;
    export sol:=NULL; #only ONE solution here.  
    export stuff;
    export mark_to_delete:=false;
end module:

SOL::list:=[]:  #where to keep the objects

for n from 1 to 5 do
    tmp:=Object(ode_solution_type);
    
    if n=3 then
        tmp:-sol:=y(x)=1;
    else
       tmp:-sol:=y(x)=0;
    fi;
    tmp:-stuff:=rand();
    SOL:=[op(SOL),tmp];
od:

In the above, I made 4 objects to have same solution which is y(x)=0 and one different. The goal is to remove all objects from the list which has duplicate solutions and keep only one copy.

for n from 1 to numelems(SOL) do
    print("sol ", n , "  is ", SOL[n]:-sol);  
od:

And this is how currently I remove the duplicates

for n from 1 to numelems(SOL) do
    for m from n+1 to numelems(SOL) do
        if is(SOL[n]:-sol=SOL[m]:-sol) and not SOL[m]:-mark_to_delete then
           print("found duplicate at ",m);
           SOL[m]:-mark_to_delete:=true;
        fi;
    od;    
od:

#now make new pass to keep the non- duplicates
new_SOL::list:=[]:
for n from 1 to numelems(SOL) do
    if not SOL[n]:-mark_to_delete then
       new_SOL:=[op(new_SOL),SOL[n]];
    fi;    
od:

checking:

for n from 1 to numelems(new_SOL) do
    print("sol ", n , "  is ", new_SOL[n]:-sol);  
od:

Does there exist a better option in Maple (while still using a list?) to remove objects in list which have the same specific field?

Could you suggest a better method?

sometimes I get a solution to an ODE, where when I plugin the initial conditions to solve for the constant of integration, the solution by inspection can be seen to be infinity.

The constant of integration is allowed to be infinity.

But now I get no solution from solve and so my program rejects this solution and says the ode can't be solved because it can't find value for the constant of integration using this initial conditions.

I am not sure how to handle this, since Maple solve will not return an infinity as possible solution for the constant of integration. I tried different assumptions.  

This has to work for general, and not for this specific example I will show now.

Here is an ODE with IC

ode:=diff(y(x),x)=y(x)^3*sin(x);
ic:=y(0)=0;
sol:=[dsolve(ode)];

The general solution is easy to find since this is just separable. The problem is when I plugin the initial conditions. (again, this is done in a program, without looking at the screen). let consider the first solution above for now.

eq:=simplify(subs( [y(x)=0,x=0],sol[1]));

And here is the problem, when next I try to solve for the constant, Maple says no solution.

solve(eq,_C1,allsolutions=true) assuming _C1::real

No solution returned.

If Maple could return _C1=infinity, then I would plugin _C1=infinity and obtain the correct particular solution which is 

simplify(subs(_C1=infinity,sol[1])) assuming real

Which is what the smart dsolve command does

ode:=diff(y(x),x)=y(x)^3*sin(x);
ic:=y(0)=0;
sol:=dsolve([ode,ic])

I do not know how Maple found the above and what algorithm it used.  

But my not too smart dsolve program gets stuck on such problems, because it can't solve for the IC.

Is there another method or option or assumptions to use, to solve such an equation and request infinity be returned as a solution?  I tried isolate(eq,_C1) but that did not work.  

One possible option to try as a last resource is to do this

limit(eq,_C1=infinity)

And this gives 0=0

which means _C1=infinity is valid solution.  And I could do this as final attempt before giving up. Is this what Maple did? But how does one know to do this in general? Unless one tries and find out? i.e I could try limit(eq,_C1=infinity) and limit(eq,_C1=-infinity) and see if any one of these gives true (after doing  evalb or is  on the output)

Any suggestions or a better/more robust method to use for such cases? I need to use the method for any equation where infinity can be a solution for the constant of integration. It will always be one equation and one variable, and can assume all are real for now if it makes it easier.

 

 

I was wondering why dsolve do not show this one solution I obtained different way, for this ode.

Maple gives 6 solutions to the ODE. But 5 of them are signular (have no constant of integration in them). I am looking at the last solution it gives, the one with constant of integration. 

But when I solve this ODE, after simplifying it, I get different general solution than the last one Maple shows. I thought they might be equivalent, but I do not see how they could be. 

So my question is why Maple did not show the simpler solution also?  Here is the code

ode_orginal:=1/3*(-2*x^(5/2)+3*y(x)^(5/3))/x^(3/2)*diff(y(x),x)/y(x)^(5/3)+1/2*(2*x^(5/2)-3*y(x)^(5/3))/x^(5/2)/y(x)^(2/3) = 0;

This looks complicated and non-linear, but when I solve for y'(x) it gives much simpler and linear ODE (Have to convert the ODE to D before solving for diff(y(x),x), since Maple can complain otherwise).

ode:=convert(ode_orginal,D);
ode:=diff(y(x),x)=solve(ode,D(y)(x),allsolutions=true);

And solving the above gives the much simpler solution

sol:=dsolve(ode,y(x));
odetest(sol,ode);

Now when using dsolve on the original complicated lookin ODE it gives

maple_sol:=[dsolve(ode_orginal)];

if there is any hope that one of the above 6 solutions will be the same as the simpler solution, it has to be the last one, with the _C1 in it, since all the others do not have _C1 (singular solutions).

But solving for y(x) from the last one does not give the simpler solution.

So it is a new general solution for the original ode and it is correct, since odetest gives zero.

I think in the process of solving for y'(x), some solutions got lost. Even though I asked for allsolutions there?.

But my main question is: Should dsolve have also have given the simpler solution? Since that one also satisfies the original nonlinear complicated ode

odetest(sol,ode_orginal);

gives 0.

Maple 2021.1

 

I solved this ode with IC, and obtained a solution.  Maple odetest says my solution satisfies the ODE but not the initial conditions. But I do not see why. When I plugin manually the initial conditions into the solution, I get true.

So I am not sure what is going on.

restart;
ode:=x*diff(y(x),x)*y(x) = (x+1)*(y(x)+1);
ic:=y(1)=1;
mysol:=y(x)-ln(y(x)+1)=-ln(2)+x+ln(x);
my_sol_at_IC:=subs([y(x)=1,x=1],mysol);

#now check manually
evalb(my_sol_at_IC);
is(my_sol_at_IC);

Using odetest gives

odetest(mysol,ode);
odetest(mysol,[ode,ic]);

What Am I doing wrong? 

Maple 2021.1

 

Edit June 12, 2021

This is another example where odetest gives different result depending on how the solution is written !

restart;
ode:=diff(y(x),x)=2/3*(y(x)-1)^(1/2):
mysol_1:=y(x) = 1/9*x^2 - 2/9*x + 10/9:
mysol_2:=y(x) - (1/9*x^2 - 2/9*x + 10/9) = 0 :
odetest(mysol_1,ode);
odetest(mysol_2,ode);

 

I can't figure what is going on here. I have an expression, where when I run the program, I see inside the debugger that the result of evalb(simplify(tmp_1)) where tmp_1 is the expression, gives false

When I copy the same tmp_1 expression to new worksheet and do evalb(simplify(tmp_1)) it gives true.

I added print statements in the code to print tmp_1 and print result of simplify(tmp_1) and for some reason, it gives false when I run the program, but it gives true when I do the same thing in a work sheet, by copying tmp_1 and trying it there.

Never seen anything like this, as expression is all numbers. Here is the print out when I run the program

Here is the same thing in worksheet

restart;
tmp_1 :=-(2*ln(-2))/7 - ln(-1)/7 = ln(1) - (2*ln(-2))/7 - ln(-1)/7;
simplify(tmp_1);
evalb(simplify(tmp_1));

                      true

and if you think I made mistake somewhere, here is also screen shot from the debugger window itself

 

again, same thing gives true in separate worksheet, copying same exact expression from above

There seem to be something loaded when I run my program that causes this difference, but I have no idea now what it is.  This has nothing to do with the debugger loaded btw, I am just using the debugger to show the details. When I run the program, without the debugger, I get false for the above, and I was trying to find why.

Any suggestions what to try and what to check for and what could cause this? Should it not give true when I run the program also? Could it be some Digits setting changed when the program run? I do not change any system defaults of any sort when running the program. 

Maple 2020.1

edit june 12, 2021

Here is another manifestation of the same problem I just found. There is something seriously wrong for it to behave this way.

This is an expression called tmp_1 in a local variable in a proc inside a module. There are not even any local symbols in it. When I run the program, all the attempts to show it is true fail. Some give FAIL and some give false when the result should be true

When I simply copy the expression from the debugger window to an open worksheet, I get true

Here is the screen shot

 

Only when I did is(expand(tmp_1)) did it give true

But when I copy the expression to the worksheet, it gives true using all the other attempts:

restart;
tmp_1:=1 = 5/4*exp(0)+1/4*piecewise(0 <= 1,-1,1 < 0,exp(-2)):
is(tmp_1);
is(simplify(tmp_1));
is((rhs-lhs)(tmp_1)=0);
is(simplify((rhs-lhs)(tmp_1)=0));
evalb(simplify((rhs-lhs)(tmp_1)=0));
is(expand(tmp_1))

I can't make a MWE so far, since these problems only show up with I run my large program. I think Maple internal memory get messed up or something else is loaded that causes these stranges problems.

For now, I added expand() to the things I should try. My current code now looks like

if is(tmp_1) or is(simplify(tmp_1)) or is((rhs-lhs)(tmp_1)=0)
               or is(simplify((rhs-lhs)(tmp_1)=0)) or evalb(simplify((rhs-lhs)(tmp_1)=0))
               or is(expand(tmp_1)) then    
   ....

Maple 2021.1

 

 

First 49 50 51 52 53 54 55 Last Page 51 of 164