1
2For people interest in Differential Equations, I've done a function that receive a string like: x^2+x^3 and put it in
32x+3x^2 witch is the differantial of the previous equation.
4
5In the code there is one thing missing: the $string{$i} is often going outOfBound (Uninitialized string offset: 6 in...)
6if your error setting is set a little too high... I just dont know how to fix this.
7
8So there is the code for differential equation with (+ and -) only:
9
10<?
11function differentiel($equa)
12{
13 $equa = strtolower($equa);
14 echo "Equation de depart: ".$equa."<br>";
15 $final = "";
16
17 for($i = 0; $i < strlen($equa); $i++)
18 {
19 //Make a new string from the receive $equa
20 if($equa{$i} == "x" && $equa{$i+1} == "^")
21 {
22 $final .= $equa{$i+2};
23 $final .= "x^";
24 $final .= $equa{$i+2}-1;
25 }
26 elseif($equa{$i} == "+" || $equa{$i} == "-")
27 {
28 $final .= $equa{$i};
29 }
30 elseif(is_numeric($equa{$i}) && $i == 0)
31 {
32 //gerer parenthese et autre terme generaux + gerer ^apres: 2^2
33 $final .= $equa{$i}."*";
34 }
35 elseif(is_numeric($equa{$i}) && $i > 0 && $equa{$i-1} != "^")
36 {
37 //gerer ^apres: 2^2
38 $final .= $equa{$i}."*";
39 }
40 elseif($equa{$i} == "^")
41 {
42 continue;
43 }
44 elseif(is_numeric($equa{$i}) && $equa{$i-1} == "^")
45 {
46 continue;
47 }
48 else
49 {
50 if($equa{$i} == "x")
51 {
52 $final .= 1;
53 }
54 else
55 {
56 $final .= $equa{$i};
57 }
58 }
59 }
60 //
61 //Manage multiplication add in the previous string $final
62 //
63 $finalMul = "";
64 for($i = 0; $i < strlen($final); $i++)
65 {
66 if(is_numeric($final{$i}) && $final{$i+1} == "*" && is_numeric($final{$i+2}))
67 {
68 $finalMul .= $final{$i}*$final{$i+2};
69 }
70 elseif($final{$i} == "*")
71 {
72 continue;
73 }
74 elseif(is_numeric($final{$i}) && $final{$i+1} != "*" && $final{$i-1} == "*")
75 {
76 continue;
77 }
78 else
79 {
80 $finalMul .= $final{$i};
81 }
82 }
83 echo "equa final: ".$finalMul;
84}
85?>
86
87I know this is not optimal but i've done this quick :)
88If you guys have any comment just email me.
89I also want to do this fonction In C to add to phpCore maybe soon...
90Patoff
91