Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2071 | zjchen | 1 | !goto $wims_read_parm |
2 | |||
3 | :def |
||
4 | title=2D quadratic regression |
||
5 | synonyme=quadratic regression, quadratic fitting |
||
6 | input=data2d |
||
7 | !exit |
||
8 | |||
9 | :proc |
||
10 | cnt=!linecnt $fml |
||
11 | pari_header=data=[$fml2];x1=sum(t=1,$cnt,data[t,1]);\ |
||
12 | x2=sum(t=1,$cnt,data[t,1]^2);\ |
||
13 | x3=sum(t=1,$cnt,data[t,1]^3);\ |
||
14 | x4=sum(t=1,$cnt,data[t,1]^4);\ |
||
15 | y0=sum(t=1,$cnt,data[t,2]);\ |
||
16 | y1=sum(t=1,$cnt,data[t,1]*data[t,2]);\ |
||
17 | y2=sum(t=1,$cnt,data[t,1]^2*data[t,2]);\ |
||
18 | sol=matsolve([x4,x3,x2;x3,x2,x1;x2,x1,$cnt],[y2;y1;y0]); |
||
19 | result=!exec pari print(sol[1,1])\ |
||
20 | print(sol[2,1])\ |
||
21 | print(sol[3,1]) |
||
22 | |||
23 | test=!linecnt $result |
||
24 | !if $test<3 |
||
25 | result= |
||
26 | !exit |
||
27 | !endif |
||
28 | !distribute lines $result into a,b,c |
||
29 | result=!rawmath $a*x^2 + $b*x + $c |
||
30 | |||
31 | l1=!line 1 of $fml |
||
32 | !distribute item $l1 into x1,y1 |
||
33 | xmin=$[$x1] |
||
34 | xmax=$[$x1] |
||
35 | ymin=$[$y1] |
||
36 | ymax=$[$y1] |
||
37 | insplot_data=$[$x1] $[$y1] |
||
38 | !for i=2 to $cnt |
||
39 | l=!line $i of $fml |
||
40 | !distribute item $l into x_,y_ |
||
41 | xmin=$[min($xmin,$x_)] |
||
42 | xmax=$[max($xmax,$x_)] |
||
43 | ymin=$[min($ymin,$y_)] |
||
44 | ymax=$[max($ymax,$y_)] |
||
45 | insplot_data=!append line $[$x_] $[$y_] to $insplot_data |
||
46 | !next i |
||
47 | |||
48 | xrange=$[max((abs($xmin)+abs($xmax))/10000,0.000000001)] |
||
49 | yrange=$[max((abs($ymin)+abs($ymax))/10000,0.000000001)] |
||
50 | xdiff=$[max($xrange,0.1*($xmax-($xmin)))] |
||
51 | x1=$[$xmin-$xdiff] |
||
52 | x2=$[$xmax+$xdiff] |
||
53 | ydiff=$[max($yrange,0.1*($ymax-($ymin)))] |
||
54 | y1=$[$ymin-$ydiff] |
||
55 | y2=$[$ymax+$ydiff] |
||
56 | |||
57 | insplot_set=size $[$picx/500], $[$picy/400] |
||
58 | !insplot [$x1:$x2] [$y1:$y2] 'insplot_data' notitle with points pt 4, $result notitle |
||
59 | ins=$ins_out |
||
60 | !if getins notin $ins |
||
61 | result= |
||
62 | !endif |
||
63 | |||
64 | !exit |
||
65 | |||
66 | :output |
||
67 | Quadratic equation: |
||
68 | !htmlmath y = $result |
||
69 | <p><center> |
||
70 | $ins |
||
71 | </center> <p> |
||
72 | Plotted data: $cnt pairs { $fml3 } |
||
73 | <p> |
||
74 | !exit |
||
75 |