Prev: Help with pcolor??? How do I use it on a rectangular grid...
Next: floating point exception when running matlab in the background
From: Nathan Jensen on 6 Jul 2010 10:15 Disclaimer: I am sorry for the long question, and confusing layout of it. Given data: x=[0 1.63 5.615 6.255 6.82 7.34 7.83 8.3 8.755 8.965]; y=[28.71 28.71 90.75 114.75 138.75 162.75 186.75 210.75 234.75 245.868]; Required output: A curve of good fit (this term is loosely defined at the moment, perhaps an R^2 value) that fits only points 2-10, with an initial slope at point 2 (1.63, 28.71) of 0. Caveats: -The slope (derivative of position curve) at point 2 (1.63, 28.71), must be 0 or as close to 0 as possible. -The only points that I really need to focus on are points 2-10. In reality, for the situation that has been presented me, any x value less than 1.63 will always have a y value of 28.71, thus the fitted curve only needs to work with x(2):x(end). -For other situations, the x values will change, but the y values will not. -I do not yet have the curve fitting toolbox, but I may in the future. First attempt: x=[0 1.63 5.615 6.255 6.82 7.34 7.83 8.3 8.755 8.965]; y=[28.71 28.71 90.75 114.75 138.75 162.75 186.75 210.75 234.75 245.868]; x0=x(2); options=optimset('TolX',1e-5,'TolFun',1e-8,'MaxFunEvals',inf); myx=fminsearch(@(a) sum((a(1)*(x-x0).^2+a(2)*(x-x0).^3+a(3)*(x-x0).^4+a(4)-y).^2), [0,0,0,0],options); x2=(x(1) : (x(end)-x(1))/1000 : x(end)); y2=myx(1)*(x2-x0).^2+myx(2)*(x2-x0).^3+myx(3)*(x2-x0).^4+myx(4); plot(x2,y2,'k-'); hold on plot(x,y,'r*'); My question to you: How can I write a function that takes in given x values, and plots a curve of good fit, with the given requirements? Is there a better way to do it than the one that I have already done? Thank you, Nathan |