forked from Aurelius-Nero/Maxima-References
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numerical-Methods.wxm
176 lines (155 loc) · 5.54 KB
/
Numerical-Methods.wxm
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
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 22.04.0 ] */
/* [wxMaxima: input start ] */
/* Date: Wed Jan 21 21:01:38 WET 2004 */
/* Contributor: Barton Willis */
/* Description: does the same as rationalize */
float2rational(x) := (
if ?floatp(x) then (
x : ?rational(x),
?numerator(x) / ?denominator(x))
else x)$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
/* Date: Tue Jan 6 08:11:19 WET 2009 */
/* Contributor: Luigi Marino */
/* Description: Implementation of bisection method */
segno(x):=block(
[],
if x=0 then return(0) else (if x>0 then return(1) else return(-1) )
)$
bisez(funz,var,a,b,eps,numiter):=block(
[fa,fb,fc,c,l,k],
define(f(var),funz),
fa:f(a),
fb:f(b),
l:abs(b-a),
k:0,
if segno(fa)#segno(fb) then do (
k:k+1,
l:l/2.0,
c:ev(a+(b-a)/2.0,float),
fc:f(c),
if (fc=0.0 or l<eps or k>numiter) then (
print("c=",c," in ",k," iterazioni"),
return(fine)
) else (
if (segno(fa)*segno(fc)>0) then (a:c, fa:fc) else (b:c, fb:fc)
)
)
)$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
/* Date: Sat Nov 26 19:17:36 WET 2005 */
/* Contributor: Robert Dodier */
/* Description: an implementation of Newton method */
NEWTONEPSILON:10.0^(-fpprec/2)$
NEWTONMAXITER:50$
mnewton(FuncList, VarList, GuessList):=
block([nfunc, Solutions, Increments, solved:false, h,
i, j, k, keepfloat:true, ratprint:false],
/* Depuration of the function arguments */
GuessList:float(GuessList),
nfunc:length(FuncList),
if length(VarList) # nfunc then (
print("mnewton: incorrect number of variable names (", nfunc,
"functions but", length(VarList), "variable names)."),
return(false)
),
if length(GuessList) # nfunc then (
print("mnewton: incorrect number of approach values (", nfunc,
"variables but", length(GuessList), "approximation values)."),
return(false)
),
apply(kill, VarList),
DF: zeromatrix (nfunc, nfunc),
h : makelist (h[i], i, 1, nfunc),
for i:1 thru nfunc do
for j:1 thru nfunc do
DF[i][j] : diff (FuncList[i], VarList[j]), /* Jacobian matrix */
GuessList: float (GuessList),
print ("DEBUG: initial GuessList:", GuessList),
/* Solution of the functions system */
for k:1 thru NEWTONMAXITER do (
Solutions:map("=", VarList, GuessList),
/* Solve 0 = F(x) + DF(x) h for h.
*/
block
([DFx: sublis (Solutions, DF),
Fx: sublis (Solutions, FuncList)],
Increments: linsolve (transpose (DFx . h + Fx)[1], h)),
GuessList:GuessList + map (rhs, Increments),
GuessList: float (GuessList),
print ("DEBUG: Increments:", Increments, "GuessList:", GuessList),
solved:true,
for i:1 thru nfunc do
solved: solved and abs (rhs (Increments[i])) < NEWTONEPSILON,
if solved then return(0)
),
/* Return of solution or error */
if solved = false then (
print("mnewton: the process doesn't converge or it converges too
slowly."),
return([])
),
Solutions:map("=", VarList, GuessList),
print ("DEBUG: sublis (Solutions, FuncList) =>", sublis (Solutions, FuncList)),
return([Solutions])
)$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
/* Date: Sun Oct 2 00:17:05 WEST 2005 */
/* Contributor: Mario Rodriguez Riotorto */
/* Description: define Lagrange interpolation */
lagrange(tab) := block([n:length(tab),sum:0,prod],
for i:1 thru n do(
prod: 1,
for k:1 thru n do
if k#i then prod: prod * ('x-tab[k][1]) / (tab[i][1]-tab[k][1]),
sum: sum + prod * tab[i][2] ),
ratsimp(sum) )$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
Fit(data, functionlist, vars) := block(
[numer:true,float:true,
lambdalist, design_matrix,
alpha_matrix, beta_vector, sol,
vector_b],
lambdalist: map(
lambda([what], apply( 'lambda, what) ),
makelist( [vars,functionlist[function_index]] , function_index, 1,
length(functionlist)) ),
design_matrix:makelist(makelist(apply(lambdalist[function_index],makelist(data[data_index][k], k, 1 , length(vars)) ),
function_index, 1, length(functionlist)),
data_index, 1, length(data)),
vector_b:makelist(
data[data_index][length(vars) + 1],
data_index, 1, length(data)),
design_matrix: apply('matrix, design_matrix),
alpha_matrix: transpose(design_matrix) . design_matrix,
beta_vector: transpose(design_matrix) . vector_b,
sol: alpha_matrix^^-1 . beta_vector,
sol: subst( '"[", 'matrix , transpose(sol) )[1],
apply("+" , sol * functionlist )
) $
data2d: [ [1, 2, 2] , [ 3, 2, 5], [1, 5, 4], [3, 5, 5],[2,1,3],[-1,0,7]] $
fit2d: Fit( data2d, [1,x,y,x*y], [x,y] );
wxdraw3d(
color = red,
point_size = 3, point_type = 7,
points(data2d),
color = black,
explicit( fit2d , x, -1, 4, y, 0, 5)
)$
/* 1d example, 1000 points */
dejta: makelist( [float(i/50), sin(i/50)+random(0.2)] , i, 0, 1000),numer$
fit:Fit( dejta, [1, x, sin(x)], [x]);
wxdraw2d(
color = blue,
points(dejta),
color = red,
explicit( fit, x, dejta[1][1]-1, dejta[length(dejta)][1]+1)
) $
/* [wxMaxima: input end ] */
/* Old versions of Maxima abort on loading files that end in a comment. */
"Created with wxMaxima 22.04.0"$