Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd really appreciate your help. The code gives me a solution, but in the end it says it cannot be used as function, which is not true.

Script:

SolveLDE[{x0_, y0_}, {x1_, y1_}] := (
ClearAll[C1, C2, a, b, c, d];
x[t_] = C1 Exp[-2 t] + C2 Exp[4 t];
y[t_] = -3 C1 Exp[-2 t] + C2 Exp[4 t];
Example1 = Solve[x[x0] == x1 && y[y0] == y1, {C1, C2}];
C1 = C1 /. Example1[[1, 1]];
C2 = C2 /. Example1[[1, 2]];
\[Lambda] = {-2, 4};
A = {{a, b}, {c, d}};
X = {x[x0], y[x0]};
dX = {x'[x0], y'[x0]};
EquSys = 
 Solve[dX == A.X && 
     y[y0] == 1/b (x'[x0] - a x[x0]) && #^2 - Tr[A] # + Det[A] == 
      0 & /@ \[Lambda], {a, b, c, d}];
{{a, b, c, d}} = {a, b, c, d} /. EquSys;
res = DSolve[{x'[t] == a x[t] + b y[t], y'[t] == c x[t] + d y[t], 
   x[x0] == x1, y[y0] == y1}, {x[t], y[t]}, t]
)
SolveLDE[{0, 0}, {0, 1}]

As solution i get...

DSolve::dsfun: -(1/4) E^(-2 t)+E^(4 t)/4 cannot be used as a function. >>
DSolve[{E^(-2 t)/2 + E^(4 t) == 
 5/2 (-(1/4) E^(-2 t) + E^(4 t)/4) + 
 3/2 ((3 E^(-2 t))/4 + E^(4 t)/4), -(3/2) E^(-2 t) + E^(4 t) == 
 1/2 (-(3/4) E^(-2 t) - E^(4 t)/4) + 
 9/2 (-(1/4) E^(-2 t) + E^(4 t)/4), True, 
True}, {-(1/4) E^(-2 t) + E^(4 t)/4, (3 E^(-2 t))/4 + E^(4 t)/4}, t]

As you can see - the solution is in the bracket....

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You made definition for x[t_] and y[t_] and then supplied to DSolve as unknown functions.

A nasty correction is this

SolveLDE[{x0_, y0_}, {x1_, y1_}] := (ClearAll[C1, C2, a, b, c, d];
  x[t_] = C1 Exp[-2 t] + C2 Exp[4 t];
  y[t_] = -3 C1 Exp[-2 t] + C2 Exp[4 t];
  Example1 = Solve[x[x0] == x1 && y[y0] == y1, {C1, C2}];
  C1 = C1 /. Example1[[1, 1]];
  C2 = C2 /. Example1[[1, 2]];
  \[Lambda] = {-2, 4};
  A = {{a, b}, {c, d}};
  X = {x[x0], y[x0]};
  dX = {x'[x0], y'[x0]};
  EquSys = 
   Solve[dX == A.X && 
       y[y0] == 1/b (x'[x0] - a x[x0]) && #^2 - Tr[A] # + Det[A] == 
        0 & /@ \[Lambda], {a, b, c, d}];
  {{a, b, c, d}} = {a, b, c, d} /. EquSys;
  res = DSolve[{xx'[t] == a xx[t] + b yy[t], 
     yy'[t] == c xx[t] + d yy[t], xx[x0] == x1, yy[y0] == y1}, {xx[t],
      yy[t]}, t])
SolveLDE[{0, 0}, {0, 1}]

I don't have time now to dig into the code and see how it can be simplified, however you should really split that into more reasonable bricks and use local variables/constants (Module/With).

share|improve this answer
add comment

Something like this may give you a better idea of how to use Mathematica for something like this:

SolveLDE[{x0_, y0_}, {x1_, y1_}] := 
 Module[{x, y, a, b, c, d, C1, C2, Example1, \[Lambda], A, X, dX, 
   EquSys, res},

  x[t_] := C1 Exp[-2 t] + C2 Exp[4 t];
  y[t_] := -3 C1 Exp[-2 t] + C2 Exp[4 t];
  Example1 = Solve[x[x0] == x1 && y[y0] == y1, {C1, C2}];
  C1 = C1 /. Example1[[1, 1]];
  C2 = C2 /. Example1[[1, 2]];
  \[Lambda] = {-2, 4};
  A = {{a, b}, {c, d}};
  X = {x[x0], y[x0]};
  dX = {x'[x0], y'[x0]};
  EquSys = 
   Solve[dX == A.X && 
       y[y0] == 1/b (x'[x0] - a x[x0]) && #^2 - Tr[A] # + Det[A] == 
        0 & /@ \[Lambda], {a, b, c, d}];
  {{a, b, c, d}} = {a, b, c, d} /. EquSys;

  res = DSolve[{x'[t] == a x[t] + b y[t], y'[t] == c x[t] + d y[t], 
     x[x0] == x1, y[y0] == y1}, {x[t], y[t]}, t]
  ]

SolveLDE[{0, 0}, {0, 1}]

DSolve[{E^(-2 t)/2 + E^(4 t) == 
   5/2 (-(1/4) E^(-2 t) + E^(4 t)/4) + 
    3/2 ((3 E^(-2 t))/4 + E^(4 t)/4), -(3/2) E^(-2 t) + E^(4 t) == 
   1/2 (-(3/4) E^(-2 t) - E^(4 t)/4) + 
    9/2 (-(1/4) E^(-2 t) + E^(4 t)/4), True, 
  True}, {-(1/4) E^(-2 t) + E^(4 t)/4, (3 E^(-2 t))/4 + E^(4 t)/4}, t]

Not a complete answer but perhaps this moves you in the right direction.

Study Module, and how to write and form functions.

Looking more closely,, when you set:

Example1 = Solve[x[x0] == x1 && y[y0] == y1, {C1, C2}];

what do you intend x0 and y0 to represent?

You use them again in the lines:

dX = {x'[x0], y'[x0]};
EquSys = Solve[dX == A.X && y[y0] == 1/b (x'[x0] - a x[x0]) && #^2 - Tr[A] # + Det[A] == 0 & /@ \[Lambda], {a, b, c, d}];

and

res = DSolve[{x'[t] == a x[t] + b y[t], y'[t] == c x[t] + d y[t], x[x0] == x1, y[y0] == y1}, {x[t], y[t]}, t]

Without getting much deeper into this than I can right now, that could cause your problems.

Update your post or make a comment to clarify.

share|improve this answer
    
This doesn't work. As pointed out above, the problem is the definition of x[t_] y[t_] and their use inside DSolve. –  Federico Mar 23 '13 at 0:55
    
I took Federico's advice into account and it worked. –  Gasper Mar 23 '13 at 1:02
    
@Federico -- No claim it works, just trying to sort through some of the confusion. –  Jagra Mar 23 '13 at 1:03
    
@Gasper -- Great, vote him up. He deserves it, but do consider better structuring your code. IT will vastly simplify your life ;-) –  Jagra Mar 23 '13 at 1:03
    
The meaning of x0,y0... What would like to achieve is to quickly get a solution for the calculations above for the already prescribed values of functions. Like for example x(0)=0 and y(0)=1. I'd like to change this values, and this is why i put "SolveLDE[{x0_, y0_}, {x1_, y1_}] := " in the first line –  Gasper Mar 23 '13 at 1:05
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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