Plotting a smooth curve from points

I'm trying to plot a smooth line for 6 points in such a way that slope at each is zero. I have successfully achieved it for middle points using 'pchip' but I also want it for extreme points i.e. x=0 and x=15 which I am unable to do while using 'pchip'.
Here is my code
clear
clc
x = [0;3;6;9;12;15];
y = [0;1.9190;-3.2287;3.5133;-2.6825;1];
xi = linspace(min(x), max(x), 150); % Evenly-Spaced Interpolation Vector
yi = interp1(x, y, xi, 'pchip');
figure
plot(x, y, 'b')
hold on
plot(xi, yi, '-r')
hold off
grid
xlabel('X')
ylabel('Y')
legend('Original Data', 'Interpolation', 'Location', 'NE')

3 个评论

How do you know that the slope is exactly zero at the knot/training points? And why do you need the slope to be zero there?
1) It seems very much zero to me. if you increase points from 150 to say 15000 and zoom in at say 3 then you would find line flattening that is one indication.
2) It is a mode shape of building. It will be used to find displacements and at max displacements your velocity is zero.
The slopes are very close to 0. Vertical lines show the original x-values without the endpoints.
x = [0;3;6;9;12;15];
y = [0;1.9190;-3.2287;3.5133;-2.6825;1];
xi = linspace(min(x), max(x), 150);
yi = interp1(x, y, xi, 'pchip');
% compute slopes
s = gradient(yi, 150);
plot(xi,s)
yline(0)
arrayfun(@xline, x(2:end-1)

请先登录,再进行评论。

 采纳的回答

Direct analytic method using piecewise cublic polynomial. The curve is first-order differentiable, but not second order differentiable (as with sublic spline or result with my first answer using BSFK)
x = [0;3;6;9;12;15];
y = [0;1.9190;-3.2287;3.5133;-2.6825;1];
[xx,is] = sort(x(:));
yy = y(is);
yy = yy(:);
dx = diff(xx);
dy = diff(yy);
y0 = yy(1:end-1);
n = numel(xx)-1;
coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
pp = struct('form', 'pp',...
'breaks', xx(:)',...
'coefs', coefs,...
'pieces', n, ...
'order', 4,...
'dim', 1);
figure
xi = linspace(min(x),max(x));
yi = ppval(pp,xi);
plot(x,y,'b-o',xi,yi,'r');
xlim([min(x),max(x)])
grid on
Propably this is how John generates his curve
x = [0 1 1.5 2 4];
y = x;

10 个评论

This one looks perfect. One thing I needed to ask that what if I need to invert axis. I swaped x with y and xi with yi but it does not seem to help.
Phi=[0,0,0,0,0;0.284629676546570,-0.830830026003772,1.30972146789057,-1.68250706566236,1.91898594722899;0.546200349457202,-1.08815592122522,0.372785597771793,1.39787738911579,-3.22870741511956;0.763521118433367,-0.594351144437141,-1.20361562377557,0.521108558113203,3.51333709166613;0.918985947228994,0.309721467890570,-0.715370323453430,-1.83083002600377,-2.68250706566236;1,1,1,1,1]
x = [-3;0;3;6;9;12;15;18];
y = [2;Phi(:,1);-2];
% ...
I mean what if data points of x are in vertical axis and data point of y are in horizontal direction.
2 & -2 are not required in this case btw
my bad I have edited it
You meant this:
Simply reverse in the plot
Phi=[0,0,0,0,0;0.284629676546570,-0.830830026003772,1.30972146789057,-1.68250706566236,1.91898594722899;0.546200349457202,-1.08815592122522,0.372785597771793,1.39787738911579,-3.22870741511956;0.763521118433367,-0.594351144437141,-1.20361562377557,0.521108558113203,3.51333709166613;0.918985947228994,0.309721467890570,-0.715370323453430,-1.83083002600377,-2.68250706566236;1,1,1,1,1]
x = [0;3;6;9;12;15];
y = [Phi(:,1)];
[xx,is] = sort(x(:));
yy = y(is);
yy = yy(:);
dx = diff(xx);
dy = diff(yy);
y0 = yy(1:end-1);
n = numel(xx)-1;
coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
pp = struct('form', 'pp',...
'breaks', xx(:)',...
'coefs', coefs,...
'pieces', n, ...
'order', 4,...
'dim', 1);
figure
xi = linspace(min(x),max(x),257);
yi = ppval(pp,xi);
plot(y,x,'bo',yi,xi,'r');
grid on
xlabel('y')
ylabel('x')
Yes thank you very much.
@Bruno Luong This code not working when I use U matrix instead of Phi. Can you identify the problem here
U =
[ 0.0099999999999999961324611071663348, 0.0099999999999109135775853669143584, 0.0099999999111141657290589619373076, 0.0099999950916249057651551477777129, 0.0099999179790382518080348086040622, 0.0099992937457678517172776594977871, 0.0099960285083040167082917113152748, 0.0099834525491509490866355612598848, 0.0099449253914255017991458157468337, 0.009846323999447003800837095284234, 0.0096285072798460927998543612544819, 0.0092037106542493232966344947090946, 0.0084606709195323459337482448750741, 0.0072820710405361979886330307220915, 0.0055742698916671948469057216179469, 0.0033040112280197171232464593122257, 0.00053175014808483579724100421936861, -0.0025712505162221429943821227521777, -0.0057329558671477912345527082542412, -0.0086218629489646264369612243760443, -0.010913709132754400729332597089299, -0.012367754792996450450179508201767, -0.012890747508422106173942911227825, -0.012567472592577966250488267599783, -0.01164515229945009724021703481774, -0.010472737297625577477688191899519, -0.0094108854489586134449001990203393, -0.0087388867080782589486161303550382, -0.0085868816101874694566219863160288, -0.0089141849213037710755013997259082, -0.0095396169756437380659534531761732, -0.010212543226020447964162999127643, -0.010699926552642461078265717227262, -0.0108599450744456334529708584838, -0.010678335066889423854438629079553, -0.010257477746216559483574134847695, -0.0097652031745608164401779235673952, -0.0093640845624847361125335454651737, -0.0091476846783579395798988663680218, -0.0091060032684093812491736268212858, -0.0091302631402275530717026685859391, -0.0090520941779876763129130280119734, -0.008699994361479501682043620007278, -0.0079511480482297064778055886135661, -0.0067606564266834858994703549612948, -0.0051608527194122140842079371355835, -0.003235971518403805549077705317905, -0.0010867949598285624203918581933532, 0.0011976376913731606458005663859032, 0.0035492402698216420991924455200202, 0.0059090834439392588268963438816099, 0.0082032477678258108275938790500788, 0.010321444428517834343889885355967, 0.012113084654043793806183239147802, 0.01340830226866463265883546214927, 0.014060498647733739900355211583222, 0.01399681671142953064815672472829, 0.013257777992260669640791471637267, 0.012009348132623758574850327502457, 0.010519299896761472133340094070721, 0.0091017507124900285233196909052446, 0.0080447472865842006001312310544592, 0.0075418032466549128209440047898747, 0.0076473359519248440597890194246514, 0.0082685277600562044800974217906565, 0.0091949987403009770783734834912755, 0.010156642152991832437254105118808, 0.010892517152642786032989632040881, 0.011211799849604805437912011785161, 0.011031473540550923425991324966651, 0.010382993134496942931009896388071, 0.0093889872484381569193319737993128, 0.0082186216048228867865059672909302, 0.007034792876125361375484254233908, 0.0059472077625274149762717775712152, 0.0049829641820615368190097200801359, 0.0040814434725701737896775721520424, 0.0031143353882182571977241036544323, 0.001925639953294891313423919757838, 0.00038162394505602417741442120294574, -0.0015820588133486667642307909750606, -0.0039287772102275116864604554973535, -0.0065142740481816147579446448258956, -0.0091083105524987711222481034757021, -0.011440410316974005303011942957641, -0.013258497205478850621812158777523, -0.014384968854700630122805343349721, -0.014754908158321164087775802101427, -0.014426062394116776230259090033398, -0.013558617295176981194679745981355, -0.012371986085750361356827204620282, -0.011092642364627231969618479590882, -0.0099089599005854841062268314694427, -0.0089453163930283857683794433414607, -0.0082597543913941130428095103419144, -0.0078603689439009801285843543746062, -0.0077289452493786600436355192362741, -0.0078388990940523147343291439242484, -0.0081588693034676870788067624559944, -0.008641519437892400768348609312414, -0.0092056109918799362172697926665756, -0.0097243182322917386055373284207968, -0.010031571762826834377452545255027, -0.0099510186087714246082331458410123, -0.0093417378655849064250836067956888, -0.0081455793032037512808485255977551, -0.0064171746039358956062747275827478, -0.004321579138226250943095053625351, -0.0020952906491651715822607113224479, 0.000019763057914839151689625961252184, 0.0018481737152786669036107903128572, 0.0033335343483726859013410734064247, 0.0045512016215525655260637329720286, 0.0056743476465245916207972296674732, 0.0069030506329083570118854387659104, 0.0083801902480250613824173966140221, 0.010123800238809067078413296966992, 0.012000805125913732696761273832022, 0.013753193658846746536226368099673, 0.015069572194744096134361917712972, 0.015679222848257755061371727381929, 0.015437946920415984517788270926172, 0.014377822051885492360846708084785, 0.012705329858299160740508164418515, 0.010749519245600698069930478591615, 0.0088777754233108531256667251239534, 0.0074059349704823016165514596188464, 0.0065291981249084996193743006604906, 0.0062913605994670081598639504146782, 0.0065961444042067048271307487430221, 0.0072510510196611480713126183773792, 0.0080257393558110777417948283806479, 0.0087057258822109543851963936259857, 0.009127614739474259205925016277837, 0.0091911532597932419098316201321187, 0.0088522813119144219392402457190571, 0.0081067589808373468629743489110167, 0.0069744908987820241040968654667445, 0.0054910215430405732528711788337558, 0.0037071147569041133033766251098209, 0.0016925413895227606255162950602321, -0.00046186030350894919251561728747922, -0.0026485965328647702805670150586131, -0.0047556469534304580425256829346245, -0.0066835542918274532447506376535181, -0.0083620200147513101269474213103236, -0.0097602055818239461058614097785409, -0.010886912946567385614013357211773, -0.011780171620740033480219814448119, -0.01248916346909069895118603490418, -0.013053733433422118191270467788234, -0.013487398276599162936848304846731, -0.013768832461682331924959308347564, -0.013844741474059205325070303083127, -0.0136443881784803749238236853364, -0.013103274137947737783479542505142, -0.012190944024149804721864039522457, -0.010935956100192868454459338956817, -0.009440276160023761746896749472377, -0.0078763344238319821932080288147964, -0.0064631754130834138027194779175485, -0.0054233773737970292278679872193274, -0.0049286809449541206086327788823772, -0.0050476766713489065481792597393649, -0.0057112698311469348050773330406972, -0.0067093125079180350851202208187356, -0.0077245012709989418434376980253342, -0.0083989695391537469727084776934392, -0.0084181511344551627122284440301976, -0.0075892839514077127364232453584089, -0.0058914109071682976714023431141951, -0.0034808836269524169786073261428499, -0.00064942087809584314176785435711578, 0.0022533894454722613914692855289883, 0.0049097300042589321427583581287369, 0.0071079323508578890105496778958302, 0.008779426623593325248364300828618, 0.0099889204675122880551461202431839, 0.010884399837544052124101321677774, 0.011626870424854274076885574143599, 0.012325666766744131287043994475446, 0.0130015991149528624587407964208, 0.013588710019021611881153107653614, 0.013970490389030477509537253189481, 0.014033807910740084267664057781457, 0.013718258420714065685368322424134, 0.013042027610520418150274969841923, 0.012095951451155124512801018335545, 0.011010771736625410340038802079429, 0.0099131243297435090014293989298293, 0.0088893741857132750754776981230399, 0.0079717883091827585782838234640482, 0.0071509002015825076665573548452255, 0.006405919259023197824496317461102, 0.0057368376148414961092221496521271, 0.0051810056152458198159977888640862, 0.0048038084672056325411238379081737, 0.0046648375905737955740210840511375, 0.0047726804335360964597350653633509, 0.005048165626492965366983724712758, 0.0053146004942622975628961034367011]
[ 0.019999999999999998423276573387057, 0.019999999936780699656116497564798, 0.01999998428748545697407482270202, 0.019999616692067626974374871258249, 0.019996427148111137850127562971966, 0.01998051962028198008106273110342, 0.019924898605818028395910492453512, 0.019773497570185349732628365463362, 0.019432369627173362173001016612767, 0.018771087347556784793137308804201, 0.017639556015977406220444955851128, 0.015900115392023606105877294751992, 0.013467822770274587667712649100258, 0.010346253934295872238694667984755, 0.0066449836465865564921975254382162, 0.0025694661631761099657430295350602, -0.0016167916575070364868696986329881, -0.0056474622380903532080979845360839, -0.0093084112465494412239456049345833, -0.012474048136142935356983802575575, -0.01511311809429381972383985947585, -0.017263706931745120283849774773641, -0.018989389387604425733737513118956, -0.020335983074038940923600021302736, -0.021307869777604613698933253218714, -0.021874204067803246098475054429913, -0.022001951365086299412852295938579, -0.021700192807120533932853847224797, -0.021053980358734732911662184396841, -0.020229188093003867092767413986453, -0.019441347693766983076539521395456, -0.018896784828643650332171633274069, -0.018727073092955776349544752940544, -0.018942341194695626675419168390792, -0.019423020736086551565672777011297, -0.019955268236237600098020755688992, -0.020298205890597585750195110883535, -0.020258287664895224049279319859762, -0.01974291175146704559768876909606, -0.018773388361295976987811723585903, -0.017453468909030866981147217234888, -0.015907402052206100561633765049196, -0.014213702541853041551314350289825, -0.012362508410639826496138842675968, -0.01025484036227315731937893595682, -0.0077451155920562023339018312009027, -0.0047107440930809502052507683375155, -0.0011215679999090885900020375322063, 0.0029183580090461036330909936068182, 0.0071736492084069843352597217859324, 0.011325216749029575505214121317067, 0.015046145629051082025884103406004, 0.018079536880051992951350242124816, 0.020292510263865258891684037516935, 0.02169062593253912900789946145872, 0.02239218673148366151600029948486, 0.022576027292006123330602076553972, 0.022424111247640396794920229718681, 0.022079115286995639656130123205167, 0.02162847432853093930012398929741, 0.021114316624106680482472120933331, 0.020558520781324307355003501026646, 0.019987717071873741222214680780355, 0.019445589187012723895859850034551, 0.018987444698124654477863506179262, 0.018660891195539007680818386178002, 0.018482618589383992838759880316633, 0.018422369157834170367049728893852, 0.018401268426094714605246224164534, 0.018305073543152343755077132536466, 0.018006808942490008349468975930656, 0.017390338199726855731695723702714, 0.016367555252999604934018481722922, 0.014885971606180759375723787415709, 0.012928164412348881391134467375222, 0.010507466335084432988513267542795, 0.0076642230930042240038878317370552, 0.0044643985748932423800041965885336, 0.00099905862191269118312042063274286, -0.0026186196625697938073124509618415, -0.0062614834002129088115136819006606, -0.0098014330098379702841706382351964, -0.013123634176421405541692923325332, -0.016136367894604385068479480842859, -0.018772188657140006754905660053481, -0.020980137003397263574610222683879, -0.022713825931510793071830483322602, -0.023923579372966422945957899617197, -0.024560244282973401487243947805757, -0.024593369829220955350758900339329, -0.024038907429631841553559437139123, -0.022984812776572100729959026200764, -0.021600454768728594528535350486286, -0.020119531653308592827998259792402, -0.018795541153680582296016774597305, -0.017840347472694413652176065342831, -0.017365217420379006694158935890632, -0.017345665894851182239143204228907, -0.017624922101834421166561911616112, -0.017957679472287529962954929598391, -0.018080886207772974689368995290075, -0.017787642759070560874649994227507, -0.016978403693722363979200676408272, -0.015671840422289710590394967074569, -0.013973029345295422069225813118563, -0.012013145874473114977727560537543, -0.009885926700059374045730365327128, -0.0076072109904735042560221570762915, -0.0051142974293102984133235345156809, -0.0023054192192761393982759808504852, 0.00089702096939224370582094026940144, 0.0044858933460921435299841612268091, 0.0083372342606519269929765436551556, 0.012221184511311595260512655575513, 0.015853417578700628225564758707393, 0.018968380594229032234374638961697, 0.021387131696753410108663567701536, 0.023055008921692394911671310526514, 0.024036678704860349966831160573529, 0.024473125706897836340001189864363, 0.02451984478264547813218052369708, 0.024291935813610654653246411096444, 0.023837550705567832404071659597204, 0.023148224559813071277107686482437, 0.022198597880097449747394284277486, 0.020995617908714241739969493565507, 0.019613557967623120489862574076454, 0.018197469578353595012239253487579, 0.016931166082480534901962349131475, 0.015980817993291005392803517362658, 0.015435555723772146213359194910591, 0.015268055864830071004805135159845, 0.015330549727009809995181247759661, 0.015388347182844445991275058327723, 0.01517930299777536916784317974708, 0.014478995059535574145259583698202, 0.013150754591263807582694314111622, 0.011166726889207702867103224605073, 0.008597493063858187410899909813589, 0.0055786953424161777861216723670107, 0.0022694790999814309423448866706704, -0.0011824472336479897875633960603125, -0.0046597907624182270842791561658973, -0.008077240428818442363218646015054, -0.011370933451189286025094341329462, -0.014484242407527924389545369335452, -0.017357227309900688684246034278919, -0.019923251984618770174725619801182, -0.022112069173122591961922395740922, -0.023856249622650691603836666119764, -0.025097965274580204755801280558629, -0.025795069739354132974136783501928, -0.025927412840210437308726428185369, -0.025504750486114464462646741993267, -0.024575927372877557900283896089083, -0.023236130043529292227410508835204, -0.02162683439255043069741513052855, -0.019923443363454229530139950004249, -0.018309195982044221042202050639637, -0.016939661362300095974233995266177, -0.015907525840094796509778666620346, -0.0152196417429911324050661400603, -0.014795723216961629916468893828365, -0.014491032743394779151921480147638, -0.014136458905460047156671338405087, -0.013582300091911641097685986075514, -0.01273015659639223757924498207395, -0.011541883369032738386685257239771, -0.010024052533831745223712604959424, -0.0081969600754643230004182455554374, -0.0060642538988253139026933225939308, -0.0035993947898600149632111659219392, -0.00075788997014279522358290461595686, 0.0024876540900776417119204390080071, 0.0061040927991432011818605230835811, 0.0099662352230042229300781889356997, 0.013854827094604159266328778721431, 0.017490756337557142316520229960527, 0.020597446014450589021871472803085, 0.022970796593957442229711966727724, 0.02453112548879647511733834628853, 0.02533687083963878582926705148877, 0.025553714215538727426870013213878, 0.025389888590517556123315273345642, 0.02502186454286028634788579229489, 0.024538725193980364142198245361007, 0.023926401261110336546316606127171, 0.023097177666324127198486412991199, 0.021951730447748568902703494632208, 0.020447719581586740422376834258932, 0.018646051658557509563585675690623, 0.016714592595694438737972292775356, 0.014885927776353377519048300869759, 0.013384014695970522166671409953282, 0.012346978872850464937057103962187, 0.011774996914969765473814359732124, 0.011522666522719051835499929828793, 0.011338445630715787586527034968526, 0.010936412853888226665307129130194, 0.010074522398138827538708473730459, 0.0086127417276206640369364452244718]
[ 0.029999999999999998656589344269851, 0.02999997209109831262051089403597, 0.029998273422333362875228258123768, 0.029981414685321059159005768079859, 0.02990352552177418646741376247119, 0.029667594015492330483575655960083, 0.029123453122290610202995979864069, 0.028091131056391765848593464972752, 0.026406666591940901795277291557129, 0.023975981775292984181956375617243, 0.020816265526553011962587108772366, 0.017066725849295901274968192222976, 0.012961024640158633986391379066168, 0.0087681787584854344376470670572338, 0.0047212824313325044808883255171252, 0.0009588045473462636097522417750204, -0.002500939953835386366223040838238, -0.0057437093435142009361586060794035, -0.0089085587976307068024809519173858, -0.012121737510684590305806919868687, -0.015439387911656841277111912803805, -0.018818664797715782287610457489009, -0.022125311160868533596650210890533, -0.025172001051186265393039970775299, -0.027771381136432878602851815895639, -0.029784470540031510537029960245231, -0.031149481402907579451635344678393, -0.031885560801765304382685378682396, -0.032075945008466560357453446263546, -0.031841316935781443974426996725742, -0.031314612715524239008781413627181, -0.030623865136991456764121766077867, -0.029882952188286913105014357185394, -0.02918510069606153670610754575373, -0.028593281727683058084456287800505, -0.028125426582190689162257836310211, -0.027738441738874953272964757285133, -0.027319767252611418145081982252884, -0.026695667430912196867403413894246, -0.025660601876778777686481147326437, -0.024023602951353719165915512619782, -0.021659264895793034876348292021492, -0.018546722325108784445220150013067, -0.014782278362909009798508920251636, -0.010559755207106032483730466122312, -0.006124033940100921355351726097534, -0.0017130078589003547027205904761653, 0.002492788187368922392662033185783, 0.0063970241361692651898297885980333, 0.0099844994029772277283402369614604, 0.01329326279105404271790903105773, 0.016375820336103470450388276781534, 0.019265833768639885153781284832935, 0.021962234854016998082143957879822, 0.024433573013452929527833450122029, 0.026636000167490634258525145986476, 0.028532879632245252066725830952866, 0.030104898102307639387957318728566, 0.031346041663952383059204464446339, 0.032249477054760188213082930888952, 0.032793802458989357780966307121113, 0.032940900532419891729396337543036, 0.03265105959324124711844937156171, 0.031911578592530849009134306343364, 0.030766270397557699317028779061563, 0.029329670834320210172804382393317, 0.0277737432034012700210264173431, 0.026285425483380981224849272798613, 0.025006328229700946502591516492977, 0.02397563127879003987759642391135, 0.023099114869958346887397937880791, 0.022159770279868240097831936373743, 0.02087098111371056456416352332847, 0.01895734758325943136484734598716, 0.016237205118171628390114524671821, 0.012679338554319773282195628007414, 0.0084151453432989679714293462021109, 0.0037032431652108910792988996258037, -0.0011398724264477136208368598260181, -0.0058184522779757101976419674586211, -0.010119300697579045980625508750435, -0.013938989702601238343796896536283, -0.017272997095239746937167656641645, -0.020177535096570515921486362053478, -0.022723380564690975180060109515344, -0.024960885549335513080574548399143, -0.026907381920049577359482005606346, -0.028556503735462317893489485159219, -0.029898970351920919806227087618067, -0.030940423170883425923643775962182, -0.031705337426631072486982982766142, -0.032224736232107702158242131180692, -0.032514948825006416245118580717394, -0.032560263333582418100449665310233, -0.032311249793076760144206915620668, -0.031703198783652450436265400486336, -0.030688952285092390992464721746989, -0.02927222363263867813004532886772, -0.02752532926928414432449953136459, -0.025580436514956206691323052492924, -0.023593998662558916828392550723136, -0.021695549154293306086260239503632, -0.01993947823855179638996103335623, -0.018278498711068815752812531788166, -0.016570023339633421860355045285298, -0.01461463181060432019832770448214, -0.012214097396808579104221314730001, -0.0092297457820197532832346925614416, -0.005622700680207215758929981707484, -0.0014651260577649315718380868261978, 0.0030775497170776739429841000959908, 0.0077838373699820878776497218503648, 0.012418585828207871898993385819969, 0.01676791771509007595653022582844, 0.020660258658598372479347465013266, 0.023974400430243698833300510953422, 0.026640444095417515611130763457256, 0.028639829882468230343941243301198, 0.030006898201397280976120218667978, 0.030829139741074698740310474226466, 0.031239874948823482057071384357472, 0.031397970075738685709641206489294, 0.031454484551425851422242183550521, 0.031513520374043569256236440340255, 0.031600250748692228336819945160935, 0.03164976731053396393274508481319, 0.031524738361311929777377158899375, 0.031059574722590466278924415146598, 0.030117908922307497170985403920354, 0.028643462212635555782560090878009, 0.026685133502243941439363327908224, 0.024385703264487106357437950225432, 0.021936868051157267183726519673395, 0.019516180007842451845322960919619, 0.017228608028830153620506491156314, 0.0150738357696736896329799163654, 0.012950610246609774732895061997738, 0.010695396236831435983559688031532, 0.0081399791296384259920359887903096, 0.0051665436055154372170904475662517, 0.0017413979758600621009802773825038, -0.0020815490497264799469159797387064, -0.006183724472503497433320861393648, -0.010417430110393905848528318651673, -0.014637100617769033379022730142487, -0.018714178824557371392211328569037, -0.0225335437278500729230425012906, -0.025979782112827262578743825789279, -0.028927265753763558087363236046697, -0.03124620454410467941945416137635, -0.032828330125632598350920940540838, -0.033624581377817383168463190546807, -0.033678521467594008554705838206103, -0.033137621349707592812900775963067, -0.032231346575233560038438046427857, -0.031217755303786221576972281265261, -0.030313801522707165336795930031106, -0.029632889184581375520844764938489, -0.029152541819537440484638873800073, -0.028725140176331824515249669441809, -0.028129219225552699587162360645321, -0.027143881828065195437390856252869, -0.025620430728643961026384290553803, -0.023526691879724684684393669469043, -0.020950069275818473600624083081599, -0.018060825646265906119137219514473, -0.015051272613082449676190449978429, -0.012074048896315054386458168036363, -0.0092009540827508005528694743717488, -0.0064141983886645742679977187683361, -0.0036288261642150515016649715867179, -0.00073396463821061709467403430456278, 0.0023643389631427136934506303047009, 0.0057133419989517315855559273558473, 0.0093035077253926267378838465330442, 0.013075395017238258975206397540119, 0.016934593261691975699680857171578, 0.020765043991821838748029983324143, 0.02443555546292586721277544473342, 0.027801090966921404870586356555323, 0.030705619994517360336183615032603, 0.03299401468184472802898322114803, 0.034536080263812218817669310504931, 0.035258421315513217724948914958727, 0.03517322656660990328035769674702, 0.034390819642246145558281782539718, 0.03310666613917737404085731295985, 0.031562321888717231845390515446888, 0.029990010316115058010715051521634, 0.02855771478941927910399467147349, 0.027332566811214273451947995366615, 0.026274243060483267673385049349645, 0.025259388739413818937793226157123, 0.024127107450007811506351629895115, 0.02272876515627265873895588757659, 0.020965384311806641740033714356279, 0.0188024292254267183030008837385, 0.01626171333543226967525812053576, 0.013399061486833773536727390261392, 0.010280471791355697776009128191065, 0.0069674199961128375718173309749089]
[ 0.039999999999999996443549605549663, 0.039993398921727810988029601410645, 0.039898311939150082846361432646748, 0.039516605525331019820545589196847, 0.038600292992578860960944507372674, 0.036943192333777630161590749461572, 0.034458214546458494540929451363443, 0.031213165364198794352746203051693, 0.027413702994114162544972821287136, 0.02334150789931275231350200943163, 0.019271375828166908454774810589198, 0.015397123986837846861468041642989, 0.011791041393119106565038674312354, 0.0084076850571991445490437458620212, 0.0051259215073018196871313835510827, 0.0018098446449347165371042770033561, -0.0016356473245175800831820013103423, -0.0052340658229447158262889305813767, -0.0089390457089380671978462885239631, -0.012662733240704527032445075514323, -0.016315177308470028549577740066944, -0.019836474490843034419106282290957, -0.023208486434337392024048250596272, -0.026442938984735269648526207958473, -0.029553144653825745935098164131752, -0.032523180253573191533436102840041, -0.035288735445535550029084238875358, -0.037738374192811670747771886863014, -0.039735298355602211264580045441735, -0.041151578690397117180054702730517, -0.041902363218364047098112361607991, -0.041968156984767330844493790917764, -0.041398145820380563878355028422381, -0.040294409510247647123180840111478, -0.038783005101624376689616760473335, -0.036981276892895473896206390075783, -0.034970721385032518598435012372952, -0.032782012111013835217011055419311, -0.030394840288840054442825604745376, -0.027751490306884038869069067368801, -0.024780370806343727719691875585033, -0.02142416082891964535224587826306, -0.017666494331940500677490717010915, -0.013550964207950430732792033522321, -0.0091868496159801178934212271374311, -0.0047378577367466661501081039474946, -0.0003937067875621032068412100932498, 0.0036707228890934865438382209253373, 0.0073389210110640936824346091851182, 0.010586032564032305462104946714218, 0.01348953878595897760959492044216, 0.016210177971529233379377311891029, 0.018944879470201137622358978380733, 0.021863431013115913012695960784679, 0.025047860352429593709903030593328, 0.028454823399295105915107913190406, 0.031915304870496584871431635833901, 0.035174073329420143865389416865213, 0.037957538384643904409549924767207, 0.040048058415757907374824282183624, 0.041339651339154303306918586200272, 0.041856127925776451249219333430753, 0.041726031539346302821834225322656, 0.041124567979617703211950688897211, 0.040204890424395296206375170685767, 0.039044819266355248362753339416447, 0.037628700671221168543010193201184, 0.035870015547029369741465896241365, 0.0336640719720718440390122347694, 0.030948162043405413713949100883435, 0.027743796030552161022673433087114, 0.024163222817509877569203464432073, 0.020377361319008439446760713818719, 0.016558421336530652165821362394273, 0.012821229381062235479754265897475, 0.0091881051514312838393068891437175, 0.0055926109102951556168981479465924, 0.001921444234103407194300811912962, -0.0019222732705642347273083700385635, -0.0059604227234370638596943210811948, -0.010110897397992066742781936193579, -0.014201563677419228208426746155189, -0.018023334515711938628467264687772, -0.021402500601804709016536270646008, -0.024264104409607304335977177956051, -0.026660852972417841745836914908592, -0.0287548489972841301961082744173, -0.030757292339851318609363869082047, -0.032847313076709168695117293224543, -0.035099097235344609794771140253863, -0.037443512533954243073184449136736, -0.039677861843602852868815567246719, -0.041520141050763910031812291901591, -0.042688793618042753263629968449599, -0.042980982404640285439114600358279, -0.042324329795451759916274024691629, -0.040787578533711951711968982442237, -0.038550306644485328229169874231561, -0.035845127490920272394892064021581, -0.032893176276998929466083967972523, -0.029853208530254914033407306127307, -0.026797460301613122666728108242839, -0.023716964317146525495020962908373, -0.020549396878986905209197323854293, -0.017216910101918585120142896039056, -0.013660947714385361692273793860626, -0.0098648182839320836093783326483148, -0.0058605356487017641604807161967046, -0.0017216871390414156923963548941324, 0.002452750698432104547197923379912, 0.0065579872374354352790862063195608, 0.010501668468718753078198347917703, 0.014217605536059484464002933199496, 0.017673063645581880376690300975679, 0.020868593485513736192391344071311, 0.023830298776365953928386823899593, 0.026595805158230938857835084773652, 0.029197027676413057066394281348266, 0.031644484419005758288502617997058, 0.033918352403864450842651008850187, 0.035969915817694218301659133937823, 0.037733561698820981819271788028954, 0.039145160836854945501268787774897, 0.040159405048021456333075908337312, 0.040758249078692135822301796711902, 0.0409457881999229857442367395005, 0.040730830307439840791236401177185, 0.040104740645522214047116083633136, 0.039025897149157829505068684258382, 0.037421072115622185755561142448593, 0.035207926596553803300413006334121, 0.032333572661299306053974835427888, 0.028815559544649931787803590418466, 0.024767659524848756917871370400775, 0.020395938446011958976698344048907, 0.015960459323542139161245583511268, 0.011711267384003692653555200106503, 0.0078188580996193259317216443151502, 0.0043241274124517321960960697676971, 0.0011281587375519616837519906873791, -0.001970914230539251476938277840176, -0.0052037967693663660271541999129525, -0.0087430593100668472357982521008456, -0.012633848626553043873255640235824, -0.01676772101866133647573020877473, -0.020910089859230108058796673980155, -0.024772377162653600807563293538319, -0.028103408744008033957374790043154, -0.030767293833545873847637872991346, -0.032779912770583427303535907803521, -0.03429144291589300830959427933707, -0.035522274987842430949481626881022, -0.036676611096342376625707231160341, -0.037865586735235971422790994343486, -0.039067217551059026064676357075539, -0.040135856163353799933468517016191, -0.040855029566596405594810480999109, -0.041011858040274320135972059570474, -0.040464619035560356283886421599142, -0.03917956471228159845847969645511, -0.037226516095230614101485608095323, -0.034739341178701721142704195959638, -0.03186049302874311664259786641555, -0.028693372892947521928800486480353, -0.025281060862052825135275046847533, -0.021617591341447272378505286271816, -0.017683846934259603308643787372302, -0.013490127655683954186023053180061, -0.0091053918912753336559175635228702, -0.0046595067557115675803226768520291, -0.00031660705976876001263421247729258, 0.0037701768550251810031146271639254, 0.0075047236659650070852833539838103, 0.010876492430433954560702513806823, 0.013960360393039692642310965765238, 0.016887390470619351217330507094731, 0.019796821454582795591162421564167, 0.022785887547336519147304962067786, 0.025874053830116465642539197745186, 0.028992445581707080505659593755223, 0.032000283323725707383261322747849, 0.034721484587336129237235372127152, 0.036989150242633163767598135158198, 0.038684734634253209122504223023333, 0.039761825687321823231313885622754, 0.040249887787099522849281067059922, 0.040238963995173502337383037260522, 0.039850631463897233888202522250025, 0.039202824948016214138088405233019, 0.038376667912757524754406719593666, 0.03739269221067411163228120339876, 0.036202148765096400002174021633085, 0.034696538282182948529718403979095, 0.03273491448301845445436990100976, 0.030184129526680491096564961933527, 0.026962855034525264680623123939982, 0.023077396041343970008001252058882, 0.01863759655932729873718971660922, 0.013845383313137338900361622718571, 0.0089561631940785330928251360767446, 0.0042222409103306957650058292418575]
[ 0.04999999999999999757138713363247, 0.049373295717149017755370418725214, 0.047570097106928033348663365995068, 0.044802368048966971354166340113512, 0.041366504333792649566493848656279, 0.037576084949682802917639810880157, 0.033697554994873061603576514660974, 0.029906297913545241825667927743765, 0.026272968593158820243670809047032, 0.022779751039349080003791961357429, 0.019357007932573154829528593035004, 0.015925657848806836451035313581559, 0.012430946990172166650637361126619, 0.0088584154879307595614919279558563, 0.0052304667757186788611253533409773, 0.0015890310398131972532097255790262, -0.0020261290781928186472375793755751, -0.0055939939907829944309839743932233, -0.0091178898010506181107332271396082, -0.012619567354850995150473275119296, -0.016125889071442684790855508908392, -0.019654533223843170763563744513069, -0.02320439507987662070020475069132, -0.026753322827798678746496520430842, -0.030262058723761209403377639426402, -0.033680496157354302995802584203, -0.036951757932762154406660087130332, -0.040011277886882653864264805521067, -0.042781159673871880253035598906663, -0.045163161002276411806657507169716, -0.047035338923243141369313240814781, -0.048256936643001212175040776086732, -0.048683616869086079081735581042256, -0.048191547812566756431632529478204, -0.046705408783140717515796258227367, -0.044223281234988523455317057861413, -0.040831307842658506131097023716947, -0.036702951393048661430957169821143, -0.032081097224508534675939223912433, -0.02724521047732890227901012122555, -0.022469325648281184695863148259587, -0.017979026553039616451190076118483, -0.013916272174877991873445312598356, -0.010319755728798565247807984823769, -0.0071255810246649876148043213641703, -0.0041888492981713171072247724191495, -0.0013220372595943244848877909380924, 0.0016581558451032440276717849769739, 0.0048863801232375064383084938002266, 0.0084175124497009009142995013263988, 0.012216779534878905352757692825971, 0.016173625978229131040135937991131, 0.020135215890593873464225853353593, 0.023949594749801693093559464865783, 0.027505366733792941176691120713782, 0.030755534257710198129878850434316, 0.033717951319950446474257854667655, 0.036452323080295554415448423812851, 0.039021458486498386957630349694548, 0.041449902406105552075694237190717, 0.043694177166072779435151800242476, 0.045635142265028107356637440683578, 0.047095642206367450076546859148718, 0.047878198644628097218548922614501, 0.047810984197478008198044837490093, 0.046787983062740099454864493855322, 0.044791838392467715327424204047047, 0.04189430928127737417318837004343, 0.038236999287633673908371606531276, 0.034001133278802011202283060720131, 0.029377437823384425235771419404607, 0.024545002554414654873538720325499, 0.019662558248403266533742370786308, 0.01486938802341147863221929026345, 0.010288823728843047073289347637104, 0.0060268022469915501251034629925929, 0.0021614251058771681087650813786472, -0.0012747078005933959376160501120179, -0.0043121052481480161289173480554382, -0.0070520737078453057369766682205281, -0.0096564504154641971022906329208402, -0.012317177432558957001239091297307, -0.015211238933996079506025950189851, -0.018453048720869417407023857502502, -0.022059018444508247240880196432533, -0.025936416757553746715409762751969, -0.029901403012448102183406389853459, -0.033721740700833793838117093122353, -0.037171506454864741933355291869656, -0.040081077123168933422066393529315, -0.042367276831786133946624890028732, -0.044035241694174065670220252419198, -0.045152951774640470781856105286778, -0.045808218877962444852208149376205, -0.046063228126875462149715425436147, -0.045921873089939795949826224080725, -0.045320308199031574734839608598058, -0.044143240950280667027349774250711, -0.04226027058131092523866195742599, -0.039570697309753969510314527274764, -0.03604331745462805709323116792886, -0.031739976938546545954535592348744, -0.026816933330553701400568097912291, -0.021504446989023177179753800250528, -0.016070514731838724455402272681126, -0.01077789422228886879768106760169, -0.0058440710630336517426462392132969, -0.0014119789831649553715113912244483, 0.0024639800478030090695501577724436, 0.0058157044165763213889111526988529, 0.0087451978860074401357334664131038, 0.011398844456237931688116597761518, 0.013937631351346238996332608378603, 0.016508752994546701481988655590971, 0.01922305785127305066515993395293, 0.022141142324902140424642063565308, 0.025268878492120493188749855151265, 0.028561210239971502378083243259277, 0.031931632720286110033275600410263, 0.035264256678872736856908583213954, 0.038425864026542999425596798535043, 0.041276627294677242119996535968385, 0.043679608626906151410697852488507, 0.045510071875015023800715736196354, 0.046665529101496647592945549165577, 0.04707626975482158814142151503912, 0.046714423805059575250340228938506, 0.04559831962516780087238312674236, 0.043788935399520511503783717910387, 0.041377051801062550540175229152329, 0.038462935084651894845236309861125, 0.035133863022956127096159439134743, 0.031446997748514185628784019937987, 0.02742469998536293794588226957476, 0.023065947520822310980300948179966, 0.018371883451085945972581624924258, 0.013377584046224705617285193059995, 0.008178257313855759769605918707569, 0.0029381418269172818576390540656895, -0.0021250638828402382272985456784786, -0.0067794915689981716530674703963255, -0.010832673146728663588378872156825, -0.014183648239560282764966207824653, -0.016856504905688871940694188722443, -0.019002738179992598966327153886535, -0.020868963958705606308874640382467, -0.022737379744387503730657096312484, -0.024855205044166078922696430653083, -0.027373049013188661092443876299285, -0.030309392351593158798682761667642, -0.033550073651557472535392851950564, -0.036880733381443768135153626896994, -0.04004037769913945553270240990118, -0.04277886081593981508274871297659, -0.044901738332839066175681088429611, -0.046291985403062357625327081361855, -0.046906976763002396161998143464533, -0.046757495399134028433020364434736, -0.045880423042740156593067516604956, -0.044316747462186751238874928351952, -0.042102145694345232413196899795213, -0.039270837420487763539788533817543, -0.035867478298759608172684501536942, -0.031958906489485081321662079609225, -0.027638498376618016874411119798705, -0.023019987631623695852078372026117, -0.018222849926820320416893939086123, -0.013355390864094393318201817871804, -0.0085027773159760546860578243888451, -0.0037250306065269536719879520103582, 0.00093450507428511870396028310414849, 0.0054345091099990440859862986244799, 0.0097221643393603859505602230589583, 0.013728036044620208454281051757562, 0.017373451863501079709198315903507, 0.020589434001301712797490223172403, 0.023341257597827253284406175126708, 0.025649717954168075103770689602417, 0.027600329228735869496584327126287, 0.029334934125501940500955339041766, 0.031025470134422966254428635779483, 0.032835166550657471830838251257174, 0.034876491548661163740276992817346, 0.037176554767702636875644373742889, 0.039659103969221511757649832607964, 0.042148303782910157663148340706494, 0.044394303294097068696101235363294, 0.046115527338040493853692832620927, 0.047048831605214628477141559914632, 0.046996912715327976480124937130878, 0.045862951912934621752837224817334, 0.043665230192181162215532509371374, 0.040528827418968884552230840662146, 0.036656636143532418952058204997257, 0.032286694401588876416134277214098, 0.02764613224060027925883353643044, 0.022912893858550090992278781831804, 0.018194396499544983248524473301577, 0.013527714176746971031450005040764, 0.0088998288413165627574932109444816, 0.0042806698988497749478674943637846]
It gives off this error
Error using symengine
Array sizes must match.
Error in sym/privBinaryOp (line 1022)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in ./ (line 349)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', 'symobj::divide');
Error in Untitled>ploty (line 178)
coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
Error in Untitled (line 159)
ploty([0;U(:,i)],[0;H])
%%%%%%%%%%%%%%%%%%%%%%%%%%%
Btw ploty is a function I made using your code. It works fine when I use Phi but not when U. I even tried copying values from U frst column and puting it in Phi and then Phi would also not work.
Sorry you are on your own if you use symbolic variables. Once you modify/adap the code it's yours.
I don't have this toolbox to help, even if I want to .
Ok no problem Thanks.

请先登录,再进行评论。

更多回答(2 个)

You could add values to the beginning and end to make the curve continuing in both directions. The example below uses hard-coded values but it wouldn't be difficult to programmatically extend the trend in the opposite y-direction on each side of X.
x = [-3;0;3;6;9;12;15;18];
% ^^ ^^ added
y = [2;0;1.9190;-3.2287;3.5133;-2.6825;1;-2];
% ^ ^^ added
xi = linspace(min(x), max(x), 150);
yi = interp1(x, y, xi, 'pchip');
% Trim the additions
rmIdx = xi<0 | xi>15;
xi(rmIdx) = [];
yi(rmIdx) = [];
x([1,end]) = [];
y([1,end]) = [];
figure
plot(x, y, 'b')
hold on
plot(xi, yi, '-r')
hold off
grid
xlabel('X')
ylabel('Y')
legend('Original Data', 'Interpolation', 'Location', 'NE')

13 个评论

That is one way to do it.
I just update the answer to add the 5 lines under "Trim the additions".
To make the solution programmatic you just need to implement these steps.
  1. Determine if the slope of + or - at the end points. That's easy. At each endpoint you just need to determine if the y-value of the end point is greater or less than the y-value of the neighboring coordinate.
  2. Compute the median x-interval: median(diff(x))
  3. Compute the median y-distance from center: median(abs(y)) (since your curve is centered at y=0)
  4. Add a coordinate to the right of the curve by adding the median x-interval to the x-value of the endpoint and set the y values as the median y-distance with its sign opposite to the end point's slope.
  5. Do the same for the left end-point by subtracting the median x-interval.
I don't think it is neccessary to add coordinates eqaul to respective axis medians. I mean if I would have added 5 and -5 instead of 2 and -2 it will still be ok. What is purpose of doing this then?
That's true. x-interval isn't important.
y is not also important if you follow 1, 4 and 5 steps without calculating medians. Thanks sir, your help is much appreciated.
Yes, that's true, too. Thanks for following up!
I can think of a few ways to do this. Adam's solution is a nice one though. +1
Hmm, as I think and re-read the question, this solution works ONLY if each point is an extremeum.
Is the target to have an interpolant that has zero slope at each point, even for a sequence of points that follow a straight line? For example, consider the points:
x = [0 1 1.5 2 4];
y = x;
plot(x,y,'-o')
Or, is this next curve your goal?
Adam's solution will not produce the latter curve in general. So which is your goal?
Hmm. I didn't think about it. You got a point there. I want the latter one actually.
How did you do that john?
@Adam Danz If I use other set of points the configuration gets distorted
I have provided the points for Phi(:,5) in the question but for Phi(:,1), Phi(:,2), Phi(:,3) & Phi(:,4) it doesn't appear to work
Change in set of points can be used from these code lines.
Phi=[0,0,0,0,0;0.284629676546570,-0.830830026003772,1.30972146789057,-1.68250706566236,1.91898594722899;0.546200349457202,-1.08815592122522,0.372785597771793,1.39787738911579,-3.22870741511956;0.763521118433367,-0.594351144437141,-1.20361562377557,0.521108558113203,3.51333709166613;0.918985947228994,0.309721467890570,-0.715370323453430,-1.83083002600377,-2.68250706566236;1,1,1,1,1]
x = [-3;0;3;6;9;12;15;18];
y = Phi(:,1);
Yeah, it's not a robust hack.
I'm following the conversation. John and Bruno have great points.
Yeah I get your point.

请先登录,再进行评论。

No extra points needed (but you might add to twist the shape of the curve in the first and last interval),
Spline order >= 8th is needed using my FEX
x = [0;3;6;9;12;15];
y = [0;1.9190;-3.2287;3.5133;-2.6825;1];
interp = struct('p', 0, 'x', x, 'v', y);
slope0 = struct('p', 1, 'x', x, 'v', 0*x);
% Download BSFK function here
% https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
pp = BSFK(x,y, 9, length(x)-1, [], struct('KnotRemoval', 'none', 'pntcon', [interp slope0]));
% Check
figure
xi = linspace(min(x),max(x));
yi = ppval(pp,xi);
plot(x,y,'b',xi,yi,'r');
for xb=pp.breaks
xline(xb);
end
grid on

4 个评论

With Adam's trick
x = [0;3;6;9;12;15];
y = [0;1.9190;-3.2287;3.5133;-2.6825;1];
% Adam's trick
xx = [-3;x;18];
yy = [2;y;-2];
interp = struct('p', 0, 'x', x', 'v', y');
slope0 = struct('p', 1, 'x', x, 'v', 0*x);
% Download BSFK function here
% https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
pp = BSFK(xx,yy, 9, length(x)-1, [], struct('KnotRemoval', 'none', 'pntcon', [interp slope0]));
% Check
figure
xi = linspace(min(x),max(x));
yi = ppval(pp,xi);
plot(x,y,'b',xi,yi,'r');
for xb=pp.breaks
xline(xb);
end
xlim([min(x),max(x)])
grid on
Monotonic data
x = [0;3;6;9;12;15];
y = x;
% Adam's trick
xx = [-3;x;18];
yy = xx;
interp = struct('p', 0, 'x', x', 'v', y');
slope0 = struct('p', 1, 'x', x, 'v', 0*x);
% Download BSFK function here
% https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
pp = BSFK(xx,yy, 9, length(x)-1, [], struct('KnotRemoval', 'none', 'pntcon', [interp slope0]));
% Check
figure
xi = linspace(min(x),max(x));
yi = ppval(pp,xi);
plot(x,y,'b-o',xi,yi,'r');
for xb=pp.breaks
xline(xb);
end
xlim([min(x),max(x)])
grid on
1st and 3rd are not the one I need. 2nd one without Adam addition would do the trick. Thanks for taking out time to answer. Very much appreciated
The shape might not meet your (un-written) expectation but it definitively meets every requiremenrs you state in the question. The interpolating solution is trully "smooth" (I believe up to the 7th derivative order is continue) with zero slope at the give abscissa.
This illustres one of the difficulty using spline fitting/interpolating: the chosen boundary conditions affects globally the shape of the interpolation.
It might be still useful for futur readers.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by