File: solvingEquations.wxm

package info (click to toggle)
wxmaxima 26.01.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,436 kB
  • sloc: cpp: 77,544; xml: 10,497; ansic: 3,651; lisp: 1,900; sh: 28; makefile: 27
file content (293 lines) | stat: -rw-r--r-- 8,002 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 20.01.2-DevelopmentSnapshot ] */
/* [wxMaxima: title   start ]
Solving equations
   [wxMaxima: title   end   ] */


/* [wxMaxima: comment start ]
Mathematics can be seen as a language consisting only of a rather small set of words and only a handful of grammatical rules. Both sets are carefully chosen, though, allowing simply reformulating the question in many cases the answer can be found.
   [wxMaxima: comment end   ] */


/* [wxMaxima: comment start ]
If you are studying mathematics the good news is that solving equations is an art, a computer can help with in many special cases, but that often relies on human creativity for finding algorithms and ways.
   [wxMaxima: comment end   ] */


/* [wxMaxima: section start ]
Simple equations
   [wxMaxima: section end   ] */


/* [wxMaxima: comment start ]
For many problems Maxima's solve() program instantly finds a list of solutions:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
pyth:c^2=a^2+b^2;
/* [wxMaxima: input   end   ] */


/* [wxMaxima: input   start ] */
sol1:solve(pyth,a);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Both solutions solve() offers are valid. We can pick any of them manually, for example:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
sol2:sol1[2];
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
By having given this equation a name ("sol2") we gained a way to re-use this equation later.
   [wxMaxima: comment end   ] */


/* [wxMaxima: comment start ]
This time, the type of solutions we can get depends on the value of a, which is why maxima asked about it. One can avoid asking questions by telling the answers to maxima's assume database:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
assume(a>0, b>0, c>0);
solve(sol2,b);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Solve also accepts lists as its arguments. In this case it will only find solutions, if the following conditions are all met:
 * The number of linearly independent equations matches the number of variables to solve.
 * The solution doesn't completely change its form depending on the range one variable is in.
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
eq2:a+2*b=10;
solve(
    [pyth,eq2],
    [a,b]
);
rootscontract(%);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
By choosing [a,b] as the list of solution variables, maxima is automatically told to eliminate these two variables on the right side of the equations, if possible.
   [wxMaxima: comment end   ] */


/* [wxMaxima: section start ]
Re-using the solutions
   [wxMaxima: section end   ] */


/* [wxMaxima: comment start ]
As seen above the last equation displayed by maxima can be referenced by using the placeholder "%":
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
%;
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Additionally haven given nearly all equations names allows us to reference the equations whenever we need them. For example we can print them out again:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
sol2;
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
We can use them as an input to solve:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
solve(sol2,b);
/* [wxMaxima: input   end   ] */
/* [wxMaxima: question  start ] */
<mth><st>Is </st><mi>a</mi><st> positive, negative or zero?</st></mth>
/* [wxMaxima: question  end   ] */
/* [wxMaxima: answer  start ] */
p;
/* [wxMaxima: answer  end   ] */


/* [wxMaxima: comment start ]
We can introduce ("substitute") them into other equations:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
E=m*c^2;
subst(pyth,%);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Additionally, the left hand side and the right hand side can be extracted using the command lhs() and rhs(), which allows to use the part left or right of the "=" in a new equation:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
lhs(pyth);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: input   start ] */
rhs(pyth);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: input   start ] */
eq10:sin(δ)=rhs(pyth);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: section start ]
Boiling it down to numbers
   [wxMaxima: section end   ] */


/* [wxMaxima: comment start ]
If in mathematics two things cancel each other out, they do so exactly. The same isn't true with IEEE floating-point numbers like the ones the computer uses:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
.1;
%-.01;
%-.1;
%+.01;
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Many computer programs are configured to show small numbers as "0" or only to show the first few digits after the decimal point hoping that the rest is a rounding error that doesn't add up (see wilkinson's polynomial for an example where this happens quickly). Maxima's solve instead tries to replace floating-point numbers by the exact fraction it most likely means and warns about doing so:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
solve(x+.1=2,x);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Since it is the general case that allows to learn about the fundamental mechanisms often a good approach is to solve everything symbolically and to keep the actual numerical input values in a separate list like the following one:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
vals:[
    a=sqrt(1/2),
    b=sqrt(1/2),
    δ=π/2
];
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
Whenever a numerical value is needed this list can be introduced into any equation:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
subst(vals,pyth);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: input   start ] */
subst(vals,eq10);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: section start ]
What if there exists no symbolical solution?
   [wxMaxima: section end   ] */


/* [wxMaxima: comment start ]
Some solutions have never been assigned a name in mathematics. One popular example would be:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
eq2:cos(x)=x;
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
solve() which tries to find a symbolical solution is bound to fail here and tells us how far it got:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
solve(eq2,x);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: comment start ]
In this case maxima offers several options to at least find the numerical values, two examples are given below:
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
wxdraw2d(
    explicit(
        rhs(eq2)-lhs(eq2),
        x,-1,1
    ),
    grid=true
)$
/* [wxMaxima: input   end   ] */


/* [wxMaxima: subsect start ]
mnewton
   [wxMaxima: subsect end   ] */


/* [wxMaxima: comment start ]
mnewton uses a modified version of newton's method for finding a solution of a nonlinear, but differentiable, statical and monotonic function near to a point (in this example 0.1)
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
load("mnewton")$
/* [wxMaxima: input   end   ] */


/* [wxMaxima: input   start ] */
mnewton(eq2,x,.1);
/* [wxMaxima: input   end   ] */


/* [wxMaxima: subsect start ]
find_root
   [wxMaxima: subsect end   ] */


/* [wxMaxima: comment start ]
find_root even works if the equation isn't differentiable and searches for a solution in between two points.
   [wxMaxima: comment end   ] */


/* [wxMaxima: input   start ] */
find_root(eq2,x,-.001,1);
/* [wxMaxima: input   end   ] */



/* Old versions of Maxima abort on loading files that end in a comment. */
"Created with wxMaxima 20.01.2-DevelopmentSnapshot"$