convert data to a function using polyfit

5 visualizaciones (últimos 30 días)
JEDIDI Ran
JEDIDI Ran el 5 de Jun. de 2020
Editada: John D'Errico el 5 de Jun. de 2020
i have used polyfit function to fit my data and to have as an output a function and i have got that plot the red one is the polyfit function , the blue is the one i have it data , as you see they are close to each other but
P(t)= =2.995134969444502*(10^3)*(t^10)+(2.454194242827056*(10^3))*(t^9)-(3.074449680687996*(10^4))*(t^8)-(1.831814183200164*(10^4))*(t^7)+(1.225116448716314*(10^5))*(t^6)+(1.225116448716314*(10^5))*(t^5)-(2.265044070414277*(10^5))*(t^4)+(6.259310423328208*(10^2))*(t^3)+(1.720638633336634*(10^5))*(t^2)+(8.587517861009773*(10^4))*t+(2.102731998692876*(10^5));
so P(t=0)= (2.102731998692876*(10^5)) different from zero
what should i do to make it close ?
  2 comentarios
KSSV
KSSV el 5 de Jun. de 2020
You should attach data here...not an image snippet. By the way, there is not data value at t= 0.
JEDIDI Ran
JEDIDI Ran el 5 de Jun. de 2020
clear; clc
time = [0 0.0799 0.0809 0.0819 0.0829 0.0839 0.0849 0.0859 0.0869 0.0879 0.0889 0.0899 0.0909 0.0919 0.0929 0.0939 0.0949 0.0959 0.0969 0.0979 0.0989 0.0999 0.1009 0.1019 0.1029 0.1039 0.1049 0.1059 0.1069 0.1079 0.1089 0.1099 0.1109 0.1119 0.1129 0.1139 0.1149 0.1159 0.1169 0.1179 0.1189 0.1199 0.1209 0.1219 0.1229 0.1239 0.1249 0.1259 0.1269 0.1279 0.1289 0.1299 0.1309 0.1319 0.1329 0.1339 0.1349 0.1359 0.1369 0.1379 0.1389 0.1399 0.1409 0.1419 0.1429 0.1439 0.1449 0.1459 0.1469 0.1479 0.1489 0.1499 0.1509 0.1519 0.1529 0.1539 0.1549 0.1559 0.1569 0.1579 0.1589 0.1599 0.1609 0.1619 0.1629 0.1639 0.1649 0.1659 0.1669 0.1679 0.1689 0.1699 0.1709 0.1719 0.1729 0.1739 0.1749 0.1759 0.1769 0.1779 0.1789 0.1799 0.1809 0.1819 0.1829 0.1839 0.1849 0.1859 0.1869 0.1879 0.1889 0.1899 0.1909 0.1919 0.1929 0.1939 0.1949 0.1959 0.1969 0.1979 0.1989 0.1999 0.2009 0.2019 0.2029 0.2039 0.2049 0.2059 0.2069 0.2079 0.2089 0.2099 0.2109 0.2119 0.2129 0.2139 0.2149 0.2159 0.2169 0.2179 0.2189 0.2199 0.2209 0.2219 0.2229 0.2239 0.2249 0.2259 0.2269 0.2279 0.2289 0.2299 0.2309 0.2319 0.2329 0.2339 0.2349 0.2359 0.2369 0.2379 0.2389 0.2399 0.2409 0.2419 0.2429 0.2439 0.2449 0.2459 0.2469 0.2479 0.2489 0.2499 0.2509 0.2519 0.2529 0.2539 0.2549 0.2559 0.2569 0.2579 0.2589 0.2599 0.2609 0.2619 0.2629 0.2639 0.2649 0.2659 0.2669 0.2679 0.2689 0.2699 0.2709 0.2719 0.2729 0.2739 0.2749 0.2759 0.2769 0.2779 0.2789 0.2799 0.2809 0.2819 0.2829 0.2839 0.2849 0.2859 0.2869 0.2879 0.2889 0.2899 0.2909 0.2919 0.2929 0.2939 0.2949 0.2959 0.2969 0.2979 0.2989 0.2999 0.3009 0.3019 0.3029 0.3039 0.3049 0.3059 0.3069 0.3079 0.3089 0.3099 0.3109 0.3119 0.3129 0.3139 0.3149 0.3159 0.3169 0.3179 0.3189 0.3199 0.3209 0.3219 0.3229 0.3239 0.3249 0.3259 0.3269 0.3279 0.3289 0.3299 0.3309 0.3319 0.3329 0.3339 0.3349 0.3359 0.3369 0.3379 0.3389 0.3399 0.3409 0.3419 0.3429 0.3439 0.3449 0.3459 0.3469 0.3479 0.3489 0.3499 0.3509 0.3519 0.3529 0.3539 0.3549 0.3559 0.3569 0.3579 0.3589 0.3599 0.3609 0.3619 0.3629 0.3639 0.3649 0.3659 0.3669 0.3679 0.3689 0.3699 0.3709 0.3719 0.3729 0.3739 0.3749 0.3759 0.3769 0.3779 0.3789 0.3799 0.3809 0.3819 0.3829 0.3839 0.3849 0.3859 0.3869 0.3879 0.3889 0.3899 0.3909 0.3919 0.3929 0.3939 0.3949 0.3959 0.3969 0.3979 0.3989 0.3999 0.4009 0.4019 0.4029 0.4039 0.4049 0.4059 0.4069 0.4079 0.4089 0.4099 0.4109 0.4119 0.4129 0.4139 0.4149 0.4159 0.4169 0.4179 0.4189 0.4199 0.4209 0.4219 0.4229 0.4239 0.4249 0.4259 0.4269 0.4279 0.4289 0.4299 0.4309 0.4319 0.4329 0.4339 0.4349 0.4359 0.4369 0.4379 0.4389 0.4399 0.4409 0.4419 0.4429 0.4439 0.4449 0.4459 0.4469 0.4479 0.4489 0.4499 0.4509 0.4519 0.4529 0.4539 0.4549 0.4559 0.4569 0.4579 0.4589 0.4599 0.4609 0.4619 0.4629 0.4639 0.4649 0.4659 0.4669 0.4679 0.4689 0.4699 0.4709 0.4719 0.4729 0.4739 0.4749 0.4759 0.4769 0.4779 0.4789 0.4799 0.4809 0.4819 0.4829 0.4839 0.4849 0.4859 0.4869 0.4879 0.4889 0.4899 0.4909 0.4919 0.4929 0.4939 0.4949 0.4959 0.4969 0.4979 0.4989 0.4999 0.5009 0.5019 0.5029 0.5039 0.5049 0.5059 0.5069 0.5079 0.5089 0.5099 0.5109 0.5119 0.5129 0.5139 0.5149 0.5159 0.5169 0.5179 0.5189 0.5199 0.5209 0.5219 0.5229 0.5239 0.5249 0.5259 0.5269 0.5279 0.5289 0.5299 0.5309 0.5319 0.5329 0.5339 0.5349 0.5359 0.5369 0.5379 0.5389 0.5399 0.5409 0.5419 0.5429 0.5439 0.5449 0.5459 0.5469 0.5479 0.5489 0.5499 0.5509 0.5519 0.5529 0.5539 0.5549 0.5559 0.5569 0.5579 0.5589 0.5599 0.5609 0.5619 0.5629 0.5639 0.5649 0.5659 0.5669 0.5679 0.5689 0.5699 0.5709 0.5719 0.5729 0.5739 0.5749 0.5759 0.5769 0.5779 0.5789 0.5799 0.5809 0.5819 0.5829 0.5839 0.5849 0.5859 0.5869 0.5879 0.5889 0.5899 0.5909 0.5919 0.5929 0.5939 0.5949 0.5959 0.5969 0.5979 0.5989 0.5999 0.6009 0.6019 0.6029 0.6039 0.6049 0.6059 0.6069 0.6079 0.6089 0.6099 0.6109 0.6119 0.6129 0.6139 0.6149 0.6159 0.6169 0.6179 0.6189 0.6199 0.6209 0.6219 0.6229 0.6239 0.6249 0.6259 0.6269 0.6279 0.6289 0.6299 0.6309 0.6319 0.6329 0.6339 0.6349 0.6359 0.6369 0.6379 0.6389 0.6399 0.6409 0.6419 0.6429 0.6439 0.6449 0.6459 0.6469 0.6479 0.6489 0.6499 0.6509 0.6519 0.6529 0.6539 0.6549 0.6559 0.6569 0.6579 0.6589 0.6599 0.6609 0.6619 0.6629 0.6639 0.6649 0.6659 0.6669 0.6679 0.6689 0.6699 0.6709 0.6719 0.6729 0.6739 0.6749 0.6759 0.6769 0.6779 0.6789 0.6799 0.6809 0.6819 0.6829 0.6839 0.6849 0.6859 0.6869 0.6879 0.6889 0.6899 0.6909 0.6919 0.6929 0.6939 0.6949 0.6959 0.6969 0.6979 0.6989 0.6999 0.7009 0.7019 0.7029 0.7039 0.7049 0.7059 0.7069 0.7079 0.7089 0.7099 0.7109 0.7119 0.7129 0.7139 0.7149 0.7159 0.7169 0.7179 0.7189 0.7199 0.7209 0.7219 0.7229 0.7239 0.7249 0.7259 0.7269 0.7279 0.7289 0.7299 0.7309 0.7319 0.7329 0.7339 0.7349 0.7359 0.7369 0.7379 0.7389 0.7399 0.7409 0.7419 0.7429 0.7439 0.7449 0.7459 0.7469 0.7479 0.7489 0.7499 0.7509 0.7519 0.7529 0.7539 0.7549 0.7559 0.7569 0.7579 0.7589 0.7599 0.7609 0.7619 0.7629 0.7639 0.7649 0.7659 0.7669 0.7679 0.7689 0.7699 0.7709 0.7719 0.7729 0.7739 0.7749 0.7759 0.7769 0.7779 0.7789 0.7799 0.7809 0.7819 0.7829 0.7839 0.7849 0.7859 0.7869 0.7879 0.7889 0.7899 0.7909 0.7919 0.7929 0.7939 0.7949 0.7959 0.7969 0.7979 0.7989 0.7999 0.8009 0.8019 0.8029 0.8039 0.8049 0.8059 0.8069 0.8079 0.8089 0.8099 0.8109 0.8119 0.8129 0.8139 0.8149 0.8159 0.8169 0.8179 0.8189 0.8199 0.8209 0.8219 0.8229 0.8239 0.8249 0.8259 0.8269 0.8279 0.8289 0.8299 0.8309 0.8319 0.8329 0.8339 0.8349 0.8359 0.8369 0.8379 0.8389 0.8399 0.8409 0.8419 0.8429 0.8439 0.8449 0.8459 0.8469 0.8479 0.8489 0.8499 0.8509 0.8519 0.8529 0.8539 0.8549 0.8559 0.8569 0.8579 0.8589 0.8599 0.8609 0.8619 0.8629 0.8639 0.8649 0.8659 0.8669 0.8679 0.8689 0.8699 0.8709 0.8719 0.8729 0.8739 0.8749 0.8759 0.8769 0.8779 0.8789 0.8799 0.8809 0.8819 0.8829 0.8839 0.8849 0.8859 0.8869 0.8879 0.8889 0.8899 0.8909 0.8919 0.8929 0.8939 0.8949 0.8959 0.8969 0.8979 0.8989 0.8999 0.9009 0.9019 0.9029 0.9039 0.9049 0.9059 0.9069 0.9079 0.9089 0.9099 0.9109 0.9119 0.9129 0.9139 0.9149 0.9159 0.9169 0.9179 0.9189 0.9199 0.9209 0.9219 0.9229 0.9239 0.9249 0.9259 0.9269 0.9279 0.9289 0.9299 0.9309 0.9319 0.9329 0.9339 0.9349 0.9359 0.9369 0.9379 0.9389 0.9399 0.9409 0.9419 0.9429 0.9439 0.9449 0.9459 0.9469 0.9479 0.9489 0.9499 0.9509 0.9519 0.9529 0.9539 0.9549 0.9559 0.9569 0.9579 0.9589 0.9599 0.9609 0.9619 0.9629 0.9639 0.9649 0.9659 0.9669 0.9679 0.9689 0.9699 0.9709 0.9719 0.9729 0.9739 0.9749 0.9759 0.9769 0.9779 0.9789 0.9799 0.9809 0.9819 0.9829 0.9839 0.9849 0.9859 0.9869 0.9879 0.9889 0.9899 0.9909 0.9919 0.9929 0.9939 0.9949 0.9959 0.9969 0.9979 0.9989 0.9999 1.0009 1.0019 1.0029 1.0039 1.0049 1.0059 1.0069 1.0079 1.0089 1.0099 1.0109 1.0119 1.0129 1.0139 1.0149 1.0159 1.0169 1.0179 1.0189 1.0199 1.0209 1.0219 1.0229 1.0239 1.0249 1.0259 1.0269 1.0279 1.0289 1.0299 1.0309 1.0319 1.0329 1.0339 1.0349 1.0359 1.0369 1.0379 1.0389 1.0399 1.0409 1.0419 1.0429 1.0439 1.0449 1.0459 1.0469 1.0479 1.0489 1.0499 1.0509 1.0519 1.0529 1.0539 1.0549 1.0559 1.0569 1.0579 1.0589 1.0599 1.0609 1.0619 1.0629 1.0639 1.0649 1.0659 1.0669 1.0679 1.0689 1.0699 1.0709 1.0719 1.0729 1.0739 1.0749 1.0759 1.0769 1.0779 1.0789 1.0799 1.0809 1.0819 1.0829 1.0839 1.0849 1.0859 1.0869 1.0879 1.0889 1.0899 1.0909 1.0919 1.0929 1.0939 1.0949 1.0959 1.0969 1.0979 1.0989 1.0999 1.1009]';
u = [0 29.541444 150.99871 279.94913 416.61618 561.22227 713.98823 875.13276 1044.8719 1223.4184 1410.981 1607.7639 1813.9661 2029.7804 2255.3929 2490.982 2736.7176 2992.7605 3259.2609 3536.358 3824.1787 4122.8367 4432.4312 4753.0461 5084.7487 5427.5883 5781.5954 6146.78 6523.1317 6910.6198 7309.1947 7718.7887 8139.3177 8570.6821 9012.7679 9465.4478 9928.5823 10402.021 10885.602 11379.156 11882.504 12395.459 12917.828 13449.411 13990.005 14539.399 15097.381 15663.733 16238.237 16820.672 17410.813 18008.438 18613.321 19225.238 19843.964 20469.275 21100.948 21738.762 22382.498 23031.937 23686.864 24347.066 25012.334 25682.461 26357.243 27036.479 27719.973 28407.532 29098.967 29794.093 30492.729 31194.698 31899.828 32607.949 33318.9 34032.519 34748.652 35467.149 36187.863 36910.652 37635.38 38361.914 39090.126 39819.891 40551.091 41283.609 42017.336 42752.164 43487.992 44224.72 44962.254 45700.504 46439.384 47178.811 47918.706 48658.994 49399.603 50140.467 50881.519 51622.699 52363.949 53105.214 53846.444 54587.588 55328.603 56069.444 56810.073 57550.451 58290.545 59030.322 59769.753 60508.81 61247.469 61985.707 62723.503 63460.84 64197.7 64934.07 65669.936 66405.288 67140.117 67874.415 68608.177 69341.399 70074.078 70806.213 71537.805 72268.854 72999.363 73729.338 74458.783 75187.704 75916.111 76644.011 77371.414 78098.331 78824.775 79550.758 80276.294 81001.398 81726.085 82450.372 83174.276 83897.816 84621.01 85343.879 86066.442 86788.722 87510.74 88232.519 88954.083 89675.456 90396.662 91117.728 91838.68 92559.544 93280.349 94001.123 94721.894 95442.692 96163.548 96884.492 97605.556 98326.772 99048.172 99769.79 100491.66 101213.82 101936.29 102659.13 103382.36 104106.01 104830.14 105554.77 106279.94 107005.69 107732.06 108459.09 109186.82 109915.29 110644.54 111374.61 112105.54 112837.38 113570.16 114303.92 115038.71 115774.56 116511.53 117249.65 117988.95 118729.49 119471.3 120214.43 120958.9 121704.77 122452.06 123200.82 123951.08 124702.88 125456.25 126211.23 126967.84 127726.12 128486.09 129247.78 130011.22 130776.42 131543.42 132312.22 133082.84 133855.29 134629.58 135405.73 136183.72 136963.56 137745.24 138528.76 139314.1 140101.25 140890.17 141680.84 142473.24 143267.31 144063.02 144860.31 145659.12 145659.12 147261.07 148064.05 148868.25 149673.59 150479.95 151287.23 152095.3 152904.05 153713.32 154522.96 155332.83 156142.75 156952.53 157761.99 158570.92 159379.1 160186.31 160992.31 161796.84 162599.64 163400.41 164198.88 164994.73 165787.63 166577.25 167363.23 168145.21 168922.8 169695.59 170463.18 171225.13 171980.99 172730.29 173472.55 174207.27 174933.94 175652.02 176360.96 177060.19 177749.12 178427.17 179093.69 179748.07 180389.66 181017.81 181631.94 182231.51 182816.04 183385.11 183938.33 184475.36 184995.92 185499.75 185986.66 186456.51 186909.21 187344.72 187763.09 188164.39 188548.79 188916.51 189267.8 189602.99 189922.42 190226.47 190515.52 190789.99 191050.27 191296.76 191529.87 191749.95 191957.38 192152.53 192335.74 192507.39 192667.84 192817.46 192956.65 193085.82 193205.39 193315.8 193417.53 193511.03 193596.79 193675.28 193746.97 193812.33 193871.8 193925.82 193974.8 194019.13 194059.17 194095.27 194127.78 194157 194183.23 194206.78 194227.92 194246.91 194264.04 194279.55 194293.69 194306.71 194318.81 194330.22 194341.11 194351.66 194362.02 194372.31 194382.63 194393.05 194403.64 194414.43 194425.45 194436.72 194448.25 194460.04 194472.11 194484.48 194497.16 194510.2 194523.62 194537.46 194551.78 194566.6 194581.96 194597.9 194614.44 194631.59 194649.34 194667.7 194686.64 194706.14 194726.15 194746.65 194767.6 194788.96 194810.7 194832.79 194855.2 194877.93 194900.96 194924.28 194947.89 194971.8 194995.99 195020.47 195045.22 195070.26 195095.55 195121.09 195146.86 195172.83 195198.98 195225.27 195251.68 195278.18 195304.73 195331.31 195357.88 195384.42 195410.91 195437.32 195463.64 195489.86 195516 195542.07 195568.09 195594.1 195620.16 195646.34 195672.7 195699.34 195726.33 195753.76 195781.72 195810.27 195839.49 195869.43 195900.13 195931.6 195963.87 195996.95 196030.83 196065.51 196100.99 196137.29 196174.42 196212.4 196251.28 196291.11 196331.95 196373.87 196416.94 196461.25 196506.86 196553.85 196602.26 196652.14 196703.53 196756.45 196810.9 196866.88 196924.38 196983.38 197043.88 197105.84 197169.25 197234.11 197300.41 197368.14 197437.31 197507.95 197580.07 197653.71 197728.91 197805.7 197884.14 197964.3 198046.25 198130.04 198215.77 198303.51 198393.34 198485.33 198579.55 198676.07 198774.94 198876.2 198979.89 199086.02 199194.6 199305.64 199419.15 199535.13 199653.6 199774.57 199898.09 200024.2 200152.98 200284.5 200418.87 200556.2 200696.62 200840.25 200987.23 201137.7 201291.79 201449.63 201611.35 201777.06 201946.87 202120.89 202299.19 202481.86 202668.98 202860.6 203056.76 203257.51 203462.87 203672.85 203887.47 204106.71 204330.58 204559.07 204792.17 205029.88 205272.21 205519.16 205770.75 206027.01 206287.97 206553.66 206824.12 207099.4 207379.53 207664.55 207954.48 208249.36 208549.19 208853.99 209163.73 209478.42 209798.02 210122.51 210451.85 210785.99 211124.87 211468.43 211816.6 212169.3 212526.47 212888.01 213253.85 213623.91 213998.11 214376.4 214758.71 215145 215535.22 215929.36 216327.4 216729.34 217135.18 217544.95 217958.67 218376.37 218798.09 219223.88 219653.79 220087.87 220526.19 220968.79 221415.73 221867.06 222322.82 222783.03 223247.73 223716.91 224190.58 224668.7 225151.24 225638.16 226129.4 226624.89 227124.57 227628.37 228136.2 228647.98 229163.65 229683.12 230206.33 230733.22 231263.72 231797.79 232335.4 232876.53 233421.17 233969.33 234521.04 235076.34 235635.27 236197.88 236764.2 237334.25 237908.03 238485.5 239066.6 239651.2 240239.15 240830.22 241424.18 242020.74 242619.58 243220.37 243822.78 244426.47 245031.08 245636.29 246241.8 246847.29 247452.51 248057.2 248661.11 249264.02 249865.75 250466.09 251064.87 251661.94 252257.17 252850.41 253441.54 254030.47 254617.08 255201.26 255782.92 256361.95 256938.25 257511.7 258082.18 258649.57 259213.74 259774.56 260331.91 260885.67 261435.74 261982.04 262524.48 263063.03 263597.66 264128.37 264655.18 265178.15 265697.33 266212.81 266724.69 267233.07 267738.08 268239.86 268738.53 269234.26 269727.19 270217.48 270705.31 271190.83 271674.22 272155.67 272635.37 273113.49 273590.24 274065.81 274540.39 275014.17 275487.34 275960.08 276432.54 276904.87 277377.23 277849.7 278322.4 278795.4 279268.75 279742.5 280216.66 280691.26 281166.29 281641.76 282117.66 282593.97 283070.69 283547.8 284025.3 284503.19 284981.46 285460.14 285939.26 286418.83 286898.93 287379.62 287860.97 288343.1 288826.1 289310.09 289795.18 290281.47 290769.07 291258.05 291748.47 292240.37 292733.74 293228.56 293724.79 294222.36 294721.19 295221.2 295722.29 296224.37 296727.36 297231.18 297735.78 298241.11 298747.16 299253.89 299761.34 300269.51 300778.45 301288.2 301798.83 302310.42 302823.07 303336.86 303851.93 304368.38 304886.33 305405.9 305927.21 306450.38 306975.52 307502.71 308032.05 308563.6 309097.43 309633.59 310172.13 310713.09 311256.49 311802.37 312350.74 312901.64 313455.09 314011.11 314569.71 315130.9 315694.67 316261 316829.86 317401.18 317974.87 318550.82 319128.88 319708.88 320290.62 320873.9 321458.51 322044.23 322630.85 323218.18 323806.04 324394.31 324982.85 325571.6 326160.49 326749.49 327338.59 327927.79 328517.1 329106.52 329696.05 330285.7 330875.44 331465.26 332055.1 332644.9 333234.59 333824.07 334413.23 335001.94 335590.05 336177.39 336763.76 337348.97 337932.79 338515.02 339095.42 339673.81 340249.98 340823.76 341395 341963.6 342529.48 343092.59 343652.93 344210.52 344765.39 345317.61 345867.24 346414.36 346959.06 347501.42 348041.51 348579.4 349115.15 349648.82 350180.46 350710.12 351237.82 351763.61 352287.52 352809.55 353329.73 353848.08 354364.59 354879.27 355392.12 355903.12 356412.26 356919.5 357424.83 357928.2 358429.57 358928.88 359426.09 359921.12 360413.91 360904.38 361392.47 361878.09 362361.18 362841.64 363319.41 363794.4 364266.56 364735.81 365202.12 365665.44 366125.78 366583.12 367037.52 367489.02 367937.71 368383.7 368827.13 369268.13 369706.86 370143.45 370578.06 371010.8 371441.77 371871.04 372298.67 372724.65 373148.98 373571.6 373992.45 374411.47 374828.57 375243.66 375656.65 376067.46 376476 376882.22 377286.06 377687.48 378086.45 378482.95 378876.99 379268.58 379657.76 380044.58 380429.11 380811.43 381191.65 381569.88 381946.26 382320.92 382694.02 383065.72 383436.19 383805.59 384174.11 384541.92 384909.22 385276.18 385643.01 386009.9 386377.06 386744.68 387113 387482.21 387852.55 388224.22 388597.47 388972.5 389349.52 389728.73 390110.31 390494.42 390881.21 391270.8 391663.26 392058.67 392457.07 392858.49 393262.95 393670.46 394081.04 394494.71 394911.49 395331.43 395754.6 396181.08 396610.95 397044.34 397481.36 397922.14 398366.8 398815.49 399268.34 399725.47 400186.99 400653.03 401123.68 401599.01 402079.11 402564.02 403053.78 403548.4 404047.87 404552.15 405061.19 405574.9 406093.19 406615.96 407143.08 407674.45 408209.96 408749.52 409293.07 409840.54 410391.92 410947.19 411506.39 412069.55 412636.71 413207.93 413783.24 414362.69 414946.32 415534.14 416126.14 416722.3 417322.57 417926.89 418535.19 419147.38 419763.34 420382.98 421006.16 421632.77 422262.68 422895.74 423531.83 424170.81 424812.52 425456.82 426103.55 426752.55 427403.67 428056.73 428711.56 429367.98 430025.81 430684.84 431344.88 432005.72 432667.14 433328.9 433990.77 434652.48 435313.77 435974.36 436633.97 437292.3 437949.05 438603.93 439256.63 439906.85 440554.3 441198.69 441839.75 442477.2 443110.77 443740.17 444365.12 444985.33 445600.47 446210.21 446814.17 447411.98 448003.18 448587.34 449163.95 449732.53 450292.56 450843.53 451384.91 451916.21 452436.94 452946.62 453444.83 453931.15 454405.21 454866.63 455315.1 455750.31 456171.97 456579.8 456973.56 457353.03 457717.96 458068.18 458403.49 458723.74 459028.78 459318.52 459592.88 459851.8 460095.27 460323.34 460536.05 460733.54 460915.94 461083.48 461236.4 461375.02 461499.7 461610.86 461708.98 461794.6 461868.33 461930.84 461982.87 462025.2 462058.69 462084.26]';
T = table(time,u);
plot(time,u,'.')
[p,~,mu] = polyfit(T.time, T.u, 10);
f = polyval(p,time,[],mu);
hold on
plot(time,f)
hold off

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 5 de Jun. de 2020
Editada: John D'Errico el 5 de Jun. de 2020
This is almost what would normally be called a broken stick regression, except the joints between the linear segments are curves and pretty smooth.
Is a polynomial ever a good choice to try to fit a curve that has essentially linear segments? Not really. Polynomials just don't like that kind of behavior, where they are SOMETIMES linear, but reasonably curved in other regions.
Can you fit a polynomial of the desired form, such that it will be forced through 0 at time 0? Well, yes. You could use my polyfitn tool. You can even trick polyfit to do so, but that would cause other problems, that you won't know how to deal with.
Simplest is to just use backslash. This code will run in R2016b or later:
P = (time.^(10:-1:1))\u;
P(end+1) = 0; % stuff a zero constant term so polyval will work
plot(time,u,'b.')
hold on
ti = linspace(0,max(time),1000);
plot(ti,polyval(P,ti),'r-')
What I have done here is to recognize that to fit a polynomial that is FORCED to pass through the point (0,0) just requires no constant term in the model. That is, the constant term must be EXACTLY zero.
As you can see, at zero, we get the desired behavior. We get exactly zero.
polyval(P,0)
ans =
0
The curve actually dips considerably below zero, but you have no data there at all. In fact, your curve fit as produced had virtually the same behavior, but your plot was not sufficiently carefully done to show that. I'll show a blowup of the plot you would have produced near zero for your fit:
As you can see, it dives down quite a way below zero. If you are worried about missing EXACTLY zero, then why are you not worried about that behavior? The small error at zero in your model was insignificant compared to the extent it dove down below zero. However, you missed that due to the way you plotted the curve, because you never evaluated the curve between the 1st and second data points!
Could I have done better? Yes, most assuredly so, but not by using a polynomial. The absolutely simplest solution is to just use a spline interpolation. Your data is not at all noisy, so an interpolating function will be a good choice. I can get that from the functions spline and ppval, or I could just have used interp1.
plot(time,u,'.b',ti,interp1(time,u,ti,'pchip'),'r-')
I used pchip to produce a curve that will NOT dip even by the slightest amount below zero there. So I could have done it like this too:
spl = pchip(time,u);
plot(time,u,'.b',ti,ppval(spl,ti),'r-')
The only thing you cannot do is write down the actual function on paper, as you have done. But there is no need to do so. The model is entirely contained in the variable spl there.
  2 comentarios
JEDIDI Ran
JEDIDI Ran el 5 de Jun. de 2020
Editada: JEDIDI Ran el 5 de Jun. de 2020
hi again,
1/ i don't inderstand that and my version is R2016a
P = (time.^(10:-1:1))\u;
P(end+1) = 0; % stuff a zero constant term so polyval will work
2/ yes ,i'm worry about alla the curve below zero
3/ i don't want only to fit the curve of my data but also to get a function to that curve , i'm wondering if interp1 can do that ?
i want to use this function in another code , there a loop and every t(i) , have to extract P(t(i))
John D'Errico
John D'Errico el 5 de Jun. de 2020
Editada: John D'Errico el 5 de Jun. de 2020
  1. That you don't understand it just means you don't understand the linear algebra that polyfit uses to generate a fit. That is irrelevant here, because while I could teach you to use bsxfun, you should not be using a polynomial fit at all here.
  2. If the fact the polynomial goes below zero is important, this essentially forces you to NOT use a polynomial, since a polynomial fit will always dip below zero in that region. Again, this is why you don't want to use a polynomial.
  3. That you don't know how to pass a spline into a function does not mean you should not use the tool. It just means you need to learn to use functions, and learn how to pass things around in MATLAB.
  4. No, you cannot extract a function "from" interp1. But I already showed you how to extract that function from pchip, for as much as you need to extract.
So the real question you need to ask is how can you pass a spline into a function? And that part is easy.
You will externally create the model using pchip. That requires ONE simple line of code.
spl = pchip(time,u);
Now you pass that spline into a function trivially enough. If the function is a nested function, you don't even need to pass it in, since the nested function can see the variables in the calling function workspace. But this will work however:
function myfun(spl)
t = linspace(0,1,10);
for i = 1:numel(t)
ppval(spl,t(i))
end
end
ans =
0
ans =
8229.47650069855
ans =
84130.8822176659
ans =
168482.726405534
ans =
194822.685396463
ans =
201555.202012822
ans =
246706.027936529
ans =
303788.903706188
ans =
364730.612310776
ans =
412693.648644221
As you can see, I created the spline, then passed it into the function, and evaluated the spline at a list of points in a loop.
You don't say what kind of function you are using it in. But anything works. For example, suppose I wanted to compute the integral of this curve?
integral(@(t) ppval(spl,t),0,time(end))
ans =
241891.376196292
So I created a function handle that uses spl. Then I passed that function handle into integral, which computed the integral of that function over the domain of your data. If your problem involves using perhaps an ODE solver, again, that is no more difficult than what I already did.
Or, for example, suppsoe I wanted to use fzero, finding the point where that curve passes through u==250?
t250 = fzero(@(t) ppval(spl,t) - 250,.5)
t250 =
0.0816728813147105
ppval(spl,t250)
ans =
250
You can always pass that spline around into any function and then use it.
Again, not only don't you need to use a polynomial here, you actively don't want to use that polynomial. What you do need to do is to use a spline. And that part is really easy, far more so than using that polynomial was, because you then typed in all of those numbers.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by