{"id":790,"date":"2023-02-08T08:52:37","date_gmt":"2023-02-08T00:52:37","guid":{"rendered":"http:\/\/www.huangrongzhen.ink\/?p=790"},"modified":"2023-02-08T08:52:38","modified_gmt":"2023-02-08T00:52:38","slug":"%e4%b8%89%e6%ac%a1%e6%a0%b7%e6%9d%a1%e6%8f%92%e5%80%bc","status":"publish","type":"post","link":"https:\/\/www.huangrongzhen.ink\/?p=790","title":{"rendered":"\u4e09\u6b21\u6837\u6761\u63d2\u503c"},"content":{"rendered":"<div class=\"wp-block-post-excerpt\"><p class=\"wp-block-post-excerpt__excerpt\">\u4f7f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u4e09\u6b21\u6837\u6761\u63d2\u503c <\/p><\/div>\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#include \"stdio.h\"\r\n#include \"malloc.h\"\r\n#include &lt;math.h>\r\n\r\n\r\nstatic int spline(int n, int end1, int end2,\r\n\tfloat slope1, float slope2,\r\n\tfloat x[], float y[],\r\n\tfloat b[], float c[], float d[],\r\n\tint* iflag)\r\n{\r\n\tint nm1, ib, i, ascend;\r\n\tfloat t;\r\n\tnm1 = n - 1;\r\n\t*iflag = 0;\r\n\tif (n &lt; 2)\r\n\t{ \/* no possible interpolation *\/\r\n\t\t*iflag = 1;\r\n\t\tgoto LeaveSpline;\r\n\t}\r\n\tascend = 1;\r\n\tfor (i = 1; i &lt; n; ++i) if (x[i] &lt;= x[i - 1]) ascend = 0;\r\n\tif (!ascend)\r\n\t{\r\n\t\t*iflag = 2;\r\n\t\tgoto LeaveSpline;\r\n\t}\r\n\tif (n >= 3)\r\n\t{\r\n\t\td[0] = x[1] - x[0];\r\n\t\tc[1] = (y[1] - y[0]) \/ d[0];\r\n\t\tfor (i = 1; i &lt; nm1; ++i)\r\n\t\t{\r\n\t\t\td[i] = x[i + 1] - x[i];\r\n\t\t\tb[i] = 2.0 * (d[i - 1] + d[i]);\r\n\t\t\tc[i + 1] = (y[i + 1] - y[i]) \/ d[i];\r\n\t\t\tc[i] = c[i + 1] - c[i];\r\n\t\t}\r\n\t\t\/* ---- Default End conditions *\/\r\n\t\tb[0] = -d[0];\r\n\t\tb[nm1] = -d[n - 2];\r\n\t\tc[0] = 0.0;\r\n\t\tc[nm1] = 0.0;\r\n\t\tif (n != 3)\r\n\t\t{\r\n\t\t\tc[0] = c[2] \/ (x[3] - x[1]) - c[1] \/ (x[2] - x[0]);\r\n\t\t\tc[nm1] = c[n - 2] \/ (x[nm1] - x[n - 3]) - c[n - 3] \/ (x[n - 2] - x[n - 4]);\r\n\t\t\tc[0] = c[0] * d[0] * d[0] \/ (x[3] - x[0]);\r\n\t\t\tc[nm1] = -c[nm1] * d[n - 2] * d[n - 2] \/ (x[nm1] - x[n - 4]);\r\n\t\t}\r\n\t\t\/* Alternative end conditions -- known slopes *\/\r\n\t\tif (end1 == 1)\r\n\t\t{\r\n\t\t\tb[0] = 2.0 * (x[1] - x[0]);\r\n\t\t\tc[0] = (y[1] - y[0]) \/ (x[1] - x[0]) - slope1;\r\n\t\t}\r\n\t\tif (end2 == 1)\r\n\t\t{\r\n\t\t\tb[nm1] = 2.0 * (x[nm1] - x[n - 2]);\r\n\t\t\tc[nm1] = slope2 - (y[nm1] - y[n - 2]) \/ (x[nm1] - x[n - 2]);\r\n\t\t}\r\n\t\t\/* Forward elimination *\/\r\n\t\tfor (i = 1; i &lt; n; ++i)\r\n\t\t{\r\n\t\t\tt = d[i - 1] \/ b[i - 1];\r\n\t\t\tb[i] = b[i] - t * d[i - 1];\r\n\t\t\tc[i] = c[i] - t * c[i - 1];\r\n\t\t}\r\n\t\t\/* Back substitution *\/\r\n\t\tc[nm1] = c[nm1] \/ b[nm1];\r\n\t\tfor (ib = 0; ib &lt; nm1; ++ib)\r\n\t\t{\r\n\t\t\ti = n - ib - 2;\r\n\t\t\tc[i] = (c[i] - d[i] * c[i + 1]) \/ b[i];\r\n\t\t}\r\n\t\tb[nm1] = (y[nm1] - y[n - 2]) \/ d[n - 2] + d[n - 2] * (c[n - 2] + 2.0 * c[nm1]);\r\n\t\tfor (i = 0; i &lt; nm1; ++i)\r\n\t\t{\r\n\t\t\tb[i] = (y[i + 1] - y[i]) \/ d[i] - d[i] * (c[i + 1] + 2.0 * c[i]);\r\n\t\t\td[i] = (c[i + 1] - c[i]) \/ d[i];\r\n\t\t\tc[i] = 3.0 * c[i];\r\n\t\t}\r\n\t\tc[nm1] = 3.0 * c[nm1];\r\n\t\td[nm1] = d[n - 2];\r\n\t}\r\n\telse\r\n\t{\r\n\t\tb[0] = (y[1] - y[0]) \/ (x[1] - x[0]);\r\n\t\tc[0] = 0.0;\r\n\t\td[0] = 0.0;\r\n\t\tb[1] = b[0];\r\n\t\tc[1] = 0.0;\r\n\t\td[1] = 0.0;\r\n\t}\r\nLeaveSpline:\r\n\treturn 0;\r\n}\r\n\r\n\r\nstatic float seval(int ni, float u,\r\n\tint n, float x[], float y[],\r\n\tfloat b[], float c[], float d[],\r\n\tint* last)\r\n{\r\n\tint i, j, k;\r\n\tfloat w;\r\n\ti = *last;\r\n\tif (i >= n - 1) i = 0;\r\n\tif (i &lt; 0) i = 0;\r\n\tif ((x[i] > u) || (x[i + 1] &lt; u))\/\/??\r\n\t{\r\n\t\ti = 0;\r\n\t\tj = n;\r\n\t\tdo\r\n\t\t{\r\n\t\t\tk = (i + j) \/ 2;\r\n\t\t\tif (u &lt; x[k]) j = k;\r\n\t\t\tif (u >= x[k]) i = k;\r\n\t\t} while (j > i + 1);\r\n\t}\r\n\t*last = i;\r\n\tw = u - x[i];\r\n\tw = y[i] + w * (b[i] + w * (c[i] + w * d[i]));\r\n\treturn (w);\r\n}\r\n\r\n\r\nvoid SPL(int n, float* x, float* y, int ni, float* xi, float* yi)\r\n{\r\n\tfloat* b, * c, * d;\r\n\tint iflag = 0, last = 0, i = 0;\r\n\tb = (float*)malloc(sizeof(float) * n);\r\n\tc = (float*)malloc(sizeof(float) * n);\r\n\td = (float*)malloc(sizeof(float) * n);\r\n\tif (!d) { printf(\"no enough memory for b,c,d\\n\"); }\r\n\telse {\r\n\t\tspline(n, 0, 0, 0, 0, x, y, b, c, d, &amp;iflag);\r\n\t\tif (iflag == 0)\r\n\t\t\tprintf(\"I got coef b,c,d now\\n\");\r\n\t\telse\r\n\t\t\tprintf(\"x not in order or other error\\n\");\r\n\t\tfor (i = 0; i &lt; ni; i++)\r\n\t\t\tyi[i] = seval(ni, xi[i], n, x, y, b, c, d, &amp;last);\r\n\r\n\t\tfree(b);\r\n\t\tfree(c);\r\n\t\tfree(d);\r\n\t};\r\n}\r\n\r\nint main()\r\n{\r\n\t\/* Sin\u63d2\u503c *\/\r\n\tfloat x[9] = { 22, 62, 77, 93, 109, 124, 140, 155, 170 };\r\n\tfloat y[9] = { 6, 50, 71, 105, 125, 128, 102, 65, 48 };\r\n\tfloat u[50] = { 0 };\r\n\tfloat s[50] = { 0 };\r\n\tint i;\r\n\r\n\tfor (i = 0; i &lt; 50; i++)\r\n\t{\r\n\t\tu[i] = 22 + i * (170 - 22) \/ 50;\r\n\t}\r\n\r\n\tSPL(9, x, y, 50, u, s);\r\n\r\n\t\/*\r\n\t\t\t\u5728\u8fd9\u91cc\u5c31\u53ef\u4ee5\u6253\u5370\u63d2\u503c\u540e\u7684\u6570\u636e\r\n\t*\/\r\n\tprintf(\"xi = [\");\r\n\tfor (i = 0; i &lt; 50; i++)\r\n\t{\r\n\t\tprintf(\"%.1f,\", u[i]);\r\n\t}\r\n\tprintf(\"];\\n\");\r\n\r\n\tprintf(\"yi = [\");\r\n\tfor (i = 0; i &lt; 50; i++)\r\n\t{\r\n\t\tprintf(\"%.f,\", s[i]);\r\n\t}\r\n\tprintf(\"];\\n\");\r\n\r\n\treturn 0;\r\n}\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u4e09\u6b21\u6837\u6761\u63d2\u503c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts\/790"}],"collection":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=790"}],"version-history":[{"count":1,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts\/790\/revisions"}],"predecessor-version":[{"id":791,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts\/790\/revisions\/791"}],"wp:attachment":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}