Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

lsfit.sh 2.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env bash
  2. # Testing gmt math for LSQFIT and SVDFIT solutions to Ax = b
  3. ps=lsfit.ps
  4. # (bottom)
  5. # Dummy data is y(x) = a + bx + c*H(x-x0) + N(0,0.1), with
  6. # a = 1, b = 0.1, c = 2, and x0 = 0.5, for x -3:3, with
  7. # N being normally distributed Gaussian noise, std = 0.1
  8. a=1
  9. b=0.1
  10. c=2
  11. x0=0.5
  12. # The next line produced stepdata.txt, now in subversion
  13. # gmt math -T-3/3/0.1 T $b MUL $a ADD $x0 STEPT $c MUL ADD 0 0.1 NRAND ADD = stepdata.txt
  14. gmt math -A@stepdata.txt+r -N4/1 -C0 1 ADD -C1 T ADD -C2 0.5 STEPT ADD -Ca LSQFIT = solution.txt
  15. # For this example we use the coefficients to evaluate the function the hard way
  16. q=($(cat solution.txt))
  17. gmt math -T-3/3/0.1 T ${q[1]} MUL ${q[0]} ADD 0.5 STEPT ${q[2]} MUL ADD = stepfit_lsq.txt
  18. gmt math -A@stepdata.txt+r -N4/1 -C0 1 ADD -C1 T ADD -C2 0.5 STEPT ADD -Ca SVDFIT = solution.txt
  19. q=($(cat solution.txt))
  20. gmt math -T-3/3/0.1 T ${q[1]} MUL ${q[0]} ADD 0.5 STEPT ${q[2]} MUL ADD = stepfit_svd.txt
  21. gmt psxy -R-3/3/0/4 -JX6.5i/4i -P -Baf -BWSne @stepdata.txt -Sc0.15c -Gblue -K -Xc > $ps
  22. gmt psxy -R -J -O -K stepfit_lsq.txt -W2p >> $ps
  23. gmt psxy -R -J -O -K stepfit_svd.txt -Sc2p -Gred >> $ps
  24. gmt pstext -R -J -O -K -F+f12p+jRB+cRB+t"Fit y(x) = a + b*x + c*H(x-$x0) + @~e@~(x)" -Dj0.2i >> $ps
  25. gmt pslegend -R -J -O -K -DjTL+w1.75i+jTL+o0.1i/0.1i -F+p << EOF >> $ps
  26. S 0.1i - 0.15i - 2p 0.3i LSQFIT solution
  27. S 0.1i c 2p red - 0.3i SVDFIT solution
  28. S 0.1i c 0.15c blue - 0.3i Input data
  29. EOF
  30. # (top)
  31. # Dummy data is y(x) = a + b*cos(2*pi*6*x/X-c) + N(0,0.1), with
  32. # a = 1, b = 2, c = 2, and X = 2, for x 0:2, with
  33. # N being normally distributed Gaussian noise, std = 0.1
  34. a=1
  35. b=2
  36. c=2
  37. X=2
  38. # The next line produced sinusoiddata.txt, now in subversion
  39. # gmt math -T0/2/0.01 T $X DIV 2 MUL PI MUL 6 MUL $c SUB COS $b MUL $a ADD 0 0.2 NRAND ADD = sinusoiddata.txt
  40. # Here, we instead use the +s modifier to return the solution evaluated at the data points.
  41. gmt math -A@sinusoiddata.txt+e+r -N4/1 -C0 1 ADD -C1,2 T ADD 2 DIV 2 MUL PI MUL 6 MUL -C1 COS -C2 SIN -Ca LSQFIT = sinusoidfit_lsq.txt
  42. gmt math -A@sinusoiddata.txt+e+r -N4/1 -C0 1 ADD -C1,2 T ADD 2 DIV 2 MUL PI MUL 6 MUL -C1 COS -C2 SIN -Ca SVDFIT = sinusoidfit_svd.txt
  43. gmt psxy -R0/2/-3/4 -J -O -Baf @sinusoiddata.txt -Sc0.15c -Gblue -K -Y5i >> $ps
  44. gmt psxy -R -J -O -K sinusoidfit_lsq.txt -W2p -i0,2 >> $ps
  45. gmt psxy -R -J -O -K sinusoidfit_svd.txt -Sc2p -Gred -i0,2 >> $ps
  46. gmt pstext -R -J -O -F+f12p+jRB+cRB+t"Fit y(x) = a + b*cos(6@~p@~x - c) + @~e@~(x) = a + b@-1@-*cos(6@~p@~x) + b@-2@-*sin(6@~p@~x) + @~e@~(x)" -Dj0.2i >> $ps
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...