Can't fitting using piecewiseline
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
function f = piecewiseLine3(x,X,a,b,c)
f = zeros(size(x, X))
if (0< b.*x <= 1) AND (0<c.*X<=1)
f = a.*((1-(x.*b)/2+(x.*b).^2./4-5.*(x.*b).^3/48+7.*(x.*b).^4./192) ./2+1./sqrt((x.*b)+1)./2)+ a.*((1-(X.*c)/2+(X.*c).^2./4-5.*(X.*c).^3/48+7.*(X.*c).^4./192) ./2+1./sqrt((X.*c)+1)./2)
elseif (0< b.*x <= 1) AND (c.*X>1)
f = a.*((1-(x.*b)/2+(x.*b).^2./4-5.*(x.*b).^3/48+7.*(x.*b).^4./192) ./2+1./sqrt((x.*b)+1)./2)+ a.*((sqrt(2./3.14159./(X.*c)).*(1-1./8./(X.*c)-3./128./(X.*c).^2-15./1024./ (X.*c).^3))./2+1./sqrt((X.*c)+1)./2)
elseif ( b.*x > 1) AND (0<c.*X<=1)
f = a.*((sqrt(2./3.14159./(x.*b)).*(1-1./8./(x.*b)-3./128./(x.*b).^2-15./1024./ (x.*b).^3))./2+1./sqrt((x.*b)+1)./2) + a.*((1-(X.*c)/2+(X.*c).^2./4-5.*(X.*c).^3/48+7.*(X.*c).^4./192) ./2+1./sqrt((X.*c)+1)./2)
else ( b.*x > 1) AND (c.*X>1)
f = a.*((sqrt(2./3.14159./(x.*b)).*(1-1./8./(x.*b)-3./128./(x.*b).^2-15./1024./ (x.*b).^3))./2+1./sqrt((x.*b)+1)./2) ++ a.*((sqrt(2./3.14159./(X.*c)).*(1-1./8./(X.*c)-3./128./(X.*c).^2-15./1024./ (X.*c).^3))./2+1./sqrt((X.*c)+1)./2)
end
end
x =[ ...
]
X =[ ...]
y =[ ...
]
ft = fittype(' piecewiseLine3(x,X,a,b,c)' )
f = fit( x, X, y, ft, 'StartPoint', [0.1 ,0.1, 0.1] )
Any suggestion?
1 comentario
dpb
el 7 de Jul. de 2021
if (0< b.*x <= 1) AND (0<c.*X<=1)
and similar aren't valid MATLAB syntax...AND is not a keyword; the logical operator for and is "&"
BUT, logical expressions aren't true unless all(0< b.*x <= 1)) is TRUE so the if block will not be executed only for those elements that are true, it will either be executed in its entirety or not at all--which more than likely will be the latter.
You would have to write the functional to evaluate using logical addressing to have all elements of the function evaluated regardless of the values according to the section.
I've never seen a case where one tried to sectionalize over a product of an estimated coefficient and the independent variable; have my doubts that will ever work; instead, "piecewise" generally separates over a range of the independent variable and recasts the form to estimate the breakpoints as part of the estimation process as well.
Respuestas (1)
Walter Roberson
el 7 de Jul. de 2021
if (0< b.*x <= 1) AND (0<c.*X<=1)
[Most of] MATLAB does not offer any range-test operation. A < x < B does not test whether x is between A and B. Instead it would be interpreted as ((A<x) < B) . The A<x would be done first, giving logical results -- 1 in the places it is true, 0 in the places it is false. Then the < B part would compare those 0 or 1 results to B.
If you want to test a range, you should code it as two comparisons, A < x & x < B
Secondly: we can tell that your x is intended to be non-scalar. When you use a non-scalar value in an if or while then MATLAB considers the condition to be satisfied if all of the values being tested are non-zero; even a single zero (false) is enough to say that the condition failed. Thus your test 0< b.*x & b.*x <= 1 would be interpreted as all(0< b.*x & b.*x <= 1) . Chances are that all of your vector tests are going to fail, because the tests are probably true for some x but false for other x. Your code is definitely not designed to be able to select the appropriate condition for each x value separately and construct the corresponding f value.
Third, MATLAB does not join tests with AND or OR like you show. MATLAB does not offer AND at all. MATLAB does offer and (lower-case) but not in the syntax you show, A AND B . MATLAB's and is a function of two variables that must be called in function form, like and(A,B) . However, MATLAB offers an inline syntax shortcut, which is & -- as in A & B such as 3 < x & x < 5
You will need to pretty much rewrite the entire logic for your function. We recommend that you use logical indexing; see https://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!