Polyfit with odd powers only?
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I am trying to get a polyfit with only odd powers - so polyfit will not work because it has odd and even.
I do not have the curve fitting toolbox.
Is there another function of lines of code that will do this? I've searched around and read topics but cant seem to get it working...
x = [-50 -49.9 -49.7 -49.6 -49.4 -49.3 -49.1 -49 -48.8 -48.7 -48.6 -48.4 -48.3 -48.1 -48 -47.8 -47.7 -47.5 -47.4 -47.2 -47.1 -47 -46.8 -46.7 -46.5 -46.4 -46.2 -46.1 -45.9 -45.8 -45.7 -45.5 -45.4 -45.2 -45.1 -44.9 -44.8 -44.6 -44.5 -44.3 -44.2 -44.1 -43.9 -43.8 -43.6 -43.5 -43.3 -43.2 -43 -42.9 -42.8 -42.6 -42.5 -42.3 -42.2 -42 -41.9 -41.7 -41.6 -41.4 -41.3 -41.2 -41 -40.9 -40.7 -40.6 -40.4 -40.3 -40.1 -40 -39.9 -39.7 -39.6 -39.4 -39.3 -39.1 -39 -38.8 -38.7 -38.5 -38.4 -38.3 -38.1 -38 -37.8 -37.7 -37.5 -37.4 -37.2 -37.1 -37 -36.8 -36.7 -36.5 -36.4 -36.2 -36.1 -35.9 -35.8 -35.6 -35.5 -35.4 -35.2 -35.1 -34.9 -34.8 -34.6 -34.5 -34.3 -34.2 -34.1 -33.9 -33.8 -33.6 -33.5 -33.3 -33.2 -33 -32.9 -32.7 -32.6 -32.5 -32.3 -32.2 -32 -31.9 -31.7 -31.6 -31.4 -31.3 -31.2 -31 -30.9 -30.7 -30.6 -30.4 -30.3 -30.1 -30 -29.8 -29.7 -29.6 -29.4 -29.3 -29.1 -29 -28.8 -28.7 -28.5 -28.4 -28.3 -28.1 -28 -27.8 -27.7 -27.5 -27.4 -27.2 -27.1 -26.9 -26.8 -26.7 -26.5 -26.4 -26.2 -26.1 -25.9 -25.8 -25.6 -25.5 -25.4 -25.2 -25.1 -24.9 -24.8 -24.6 -24.5 -24.3 -24.2 -24 -23.9 -23.8 -23.6 -23.5 -23.3 -23.2 -23 -22.9 -22.7 -22.6 -22.5 -22.3 -22.2 -22 -21.9 -21.7 -21.6 -21.4 -21.3 -21.1 -21];
y = [-24.75097 -24.67164 -24.4801 -24.3577 -24.16221 -24.07226 -23.8446 -23.74222 -23.54947 -23.44198 -23.34674 -23.1479 -23.02214 -22.84665 -22.73298 -22.54093 -22.42616 -22.22016 -22.12129 -21.92824 -21.83178 -21.72892 -21.51621 -21.42578 -21.21406 -21.11511 -20.91655 -20.82348 -20.60448 -20.51302 -20.41675 -20.21547 -20.11611 -19.92628 -19.81589 -19.62125 -19.51582 -19.31288 -19.21443 -19.01194 -18.91046 -18.82235 -18.62306 -18.52276 -18.31691 -18.21565 -18.01884 -17.91986 -17.7219 -17.62982 -17.53392 -17.32261 -17.22725 -17.02708 -16.92875 -16.72639 -16.62719 -16.42994 -16.32574 -16.12839 -16.03569 -15.93901 -15.73049 -15.62979 -15.44024 -15.33308 -15.14062 -15.04554 -14.83767 -14.74988 -14.64451 -14.44684 -14.34319 -14.14475 -14.05439 -13.85229 -13.75652 -13.55815 -13.45882 -13.25949 -13.17189 -13.06923 -12.8671 -12.77876 -12.57822 -12.48735 -12.29286 -12.19462 -12.00186 -11.91447 -11.81787 -11.63201 -11.54073 -11.34952 -11.27196 -11.0899 -11.00039 -10.82415 -10.74487 -10.57193 -10.49104 -10.40562 -10.24737 -10.16286 -10.00381 -9.92581 -9.77272 -9.70086 -9.53765 -9.47072 -9.39156 -9.24676 -9.17277 -9.02883 -8.96238 -8.82083 -8.75691 -8.62635 -8.5598 -8.44304 -8.38449 -8.32837 -8.21934 -8.16412 -8.05312 -8.00186 -7.90721 -7.85077 -7.74586 -7.69695 -7.63831 -7.53163 -7.48072 -7.37289 -7.32272 -7.21067 -7.15707 -7.05749 -7.01419 -6.9207 -6.88097 -6.84193 -6.77068 -6.74107 -6.6824 -6.65511 -6.60911 -6.58551 -6.54557 -6.51723 -6.503 -6.46382 -6.44574 -6.39763 -6.38649 -6.34661 -6.33092 -6.29201 -6.27877 -6.2389 -6.22502 -6.20754 -6.17015 -6.15153 -6.11958 -6.10592 -6.06368 -6.05479 -6.023 -6.00659 -5.99346 -5.96016 -5.94925 -5.9124 -5.90166 -5.87321 -5.85589 -5.82928 -5.82597 -5.7912 -5.77671 -5.76612 -5.73985 -5.731 -5.71439 -5.70532 -5.67559 -5.66804 -5.6418 -5.63944 -5.63151 -5.61857 -5.60494 -5.58464 -5.58045 -5.55205 -5.54998 -5.52862 -5.52516 -5.50865 -5.50969];
Please can someone assist
0 commentaires
Réponse acceptée
Torsten
le 8 Mai 2018
Modifié(e) : Torsten
le 8 Mai 2018
x = [ ... ];
y = [ ... ];
x = x.';
y = y.';
n = 5; % degree of polynomial p ; n must be odd
A = [];
for i = 1:2:n
A = [A x.^i];
end
coeff = A\y % Polynomial is given by p(z)=coeff(1)*z + coeff(2)*z^3 + ... + coeff((n+1)/2)*z^n
Best wishes
Torsten.
2 commentaires
Guillaume
le 8 Mai 2018
You can construct A more simply:
A = x(:) .^ (1:2:n); %R2016b or later
A = bsxfun(@power, x(:), 1:2:n); %pre-R2016b
Plus de réponses (1)
Rik
le 8 Mai 2018
%set initial estimate
intial_b_vals=[1 5];
x = rand(10,1);%x-values
yx = rand(10,1);%measured y(x)
%create function (must support vector input)
a=(1:(2*length(intial_b_vals)-1));
a(2,1==mod(a,2))=1:length(intial_b_vals);
str=sprintf('b(%d)*x.^%d+',a([2 1],:));
str=strrep(str,'b(0)','0');
str(end)='';
fun=eval(['@(b,x) ' str]);
%set options and perform actual fit
%Ordinary Least Squares cost function
OLS = @(b) sum((y(b,x) - yx).^2);
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
%Use 'fminsearch' to minimise the 'OLS' function
fit_output = fminsearch(OLS, intial_b_vals, opts);
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!