Posts Tagged ‘diff’

Convincing maple to solve an ODE with Neumann conditions at a symbolic valued location

Friday, November 17th, 2017

I can use maple to solve a 1D second-order ODE with Dirichlet boundary conditions at symbolic-valued locations:

# Z'' = 0, Z(a)=0, Z(b) = 1
dsolve({diff(Z(r),r,r) = 0,Z(a)=0,Z(b)=1}); 

This correctly returns

                  r       a
       Z(r) = - ----- + -----
                a - b   a - b

I can also easily convince maple to solve this ODE with some Neumann (normal derivative) boundary conditions at at fixed-value, numeric location:

# Z'' = 0, Z(a) = 1, Z'(0) = 0
dsolve({diff(Z(r),r,r) = 0,Z(a)=1,eval(diff(Z(r),r),r=0)=0});

produces

                 Z(r) = 1

But if I try naively to use a Neumann condition at a symbolic value location

# Z'' = 0, Z(a) = 1, Z'(b) = 0
dsolve({diff(Z(r),r,r) = 0,Z(a)=1,eval(diff(Z(r),r),r=b)=0});

then I get an error:

Error, (in dsolve) found differentiated functions with same name but depending on different arguments in the given DE system: {Z(b), Z(r)}

After a long hunt, I found the solution. dsolve takes an optional second argument that can tell it what the dependent variable actually is. So the correct call is:

# Z'' = 0, Z(a) = 1, Z'(b) = 0
dsolve({diff(Z(r),r,r) = 0,Z(a)=1,eval(diff(Z(r),r),r=b)=0});

and this gives the correct answer

                 Z(r) = 1

Energy optimization, calculus of variations, Euler Lagrange equations in Maple

Tuesday, August 16th, 2016

Here’s a simple demonstration of how to solve an energy functional optimization symbolically using Maple.

Suppose we’d like to minimize the 1D Dirichlet energy over the unit line segment:

min  1/2 * f'(t)^2
 f
subject to: f(0) = 0, f(1) = 1

we know that the solution is given by solving the differential equation:

f''(t) = 0, f(0) = 0, f(1) = 1

and we know that solution to be

f(t) = t

How do we go about verifying this in Maple:

with(VariationalCalculus):
E := diff(f(t),t)^2:
L := EulerLagrange(E,t,f(t)):

so far this will output:

        L := {-2*diff(diff(x(t),t),t), -diff(x(t),t)^2 = K[2], 2*diff(x(t),t) = K[1]}

Finally solve with the boundary conditions using:

dsolve({L[1],f(0)=0,f(1)=1});

which will output

         t(t) = t

atan2 is harmonic

Thursday, August 22nd, 2013

Don’t you forget it. For the lazy among us, here’s a maple proof:

simplify(diff(arctan(y,x),x,x)+diff(arctan(y,x),y,y),size);

produces:

0

Remove all duplicate songs/mp3s iTunes creates using bash script

Tuesday, September 22nd, 2009

I gave up using iTunes to play music about year ago, but I haven’t found a free alternative to iTunes’s exceptional file management based on mp3 ID3 tags (If you know of one — I mean better than iTunes one — let me know). So occasionally I let iTunes organize my music library. I drop in folders containing new music and let iTunes go at it. The problem is that if I have folders containing an mp3 and an m3u playlist I get duplicates. If I don’t notice this right away the duplicates build up.

Here’s a bash script to delete all true duplicates. The files must be exactly the same and have almost the same name (the difference being the number iTunes appends on a copy: “song.mp3” becomes “song 1.mp3”).

Verbose version:


#!/bin/bash
find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
do
  original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
  # check that  the hypothetical original exists
  if [ -e "$original" ];then
    # check that the original is exactly the same file as the copy
    if diff "$original" "$copy" >/dev/null ;then
      rm "$copy"
      echo "$copy deleted..."
    else
      echo "$copy is different..."
      echo "  $copy not deleted..."
    fi
  else
    echo "$original does not exist..."
    echo "  $copy not deleted..."
  fi
done

Quiet Version


#!/bin/bash
find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
do
  original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
  # check that  the hypothetical original exists
  if [ -e "$original" ];then
    # check that the original is exactly the same file as the copy
    if diff "$original" "$copy" >/dev/null ;then
      rm "$copy"
    fi
  fi
done