- scattered x, y values (pairs are important but x values are not ordered at all)
- a number of broken line segments, and interpolation needs to happen for each line segment with extrapolation if necessary
- a number of broken line segments, and for any line segment that does not cross the target value, a NaN should be returned -- one return value for each line segment
- a number of broken line segments, and the output should list only matches with no return value for line segments that do not match
- a number of broken line segments, and the output should be any single value that matches
- a number of broken line segments, and the output should be the first value that matches
- continuous non-crossing lines that happen to travel back and forth on the x axes
- continuous lines that might happen to cross each other that happen to travel back and forth on the x axes
How to use "predict" to get x value for a given y value?
42 次查看(过去 30 天)
显示 更早的评论
I found predict but it seems to generate y values for a given x value. Is there another way to get the x value for a given y value i.e. when y = 0 what is x.
When I manually examine the data, the x value increases while the y generally trends downwards. However, the y value occassionally spikes and dips below 0, but the general trend is decreasing.
4 个评论
Walter Roberson
2024-12-4,9:09
"Scattered X and y values with X increasing." does not match "My data have duplicate x values". Not unless "with X increasing" you mean X is monotonic but not "strictly" monotonic.
回答(3 个)
John D'Errico
2024-12-3,20:27
编辑:John D'Errico
2024-12-3,20:35
Huh? Of course polyfit works. It does NOT require the x values be distinct. I'm not at all sure why you think that is necessary. Perhaps you are thinking of tools like interp1, or an interpolating spline. Regardless, polyfit does not provide an inverse computation. Nor does fit. Nor does predict, or any such tool.
And of course, the solution you want will not be unique. That will be the case for almost any nonlinear function.
What does this mean? It means you need to use a rootfinder. fzero is your best choice in general. But fsolve is an alternative. Again, since multiple solutions are potentially possible, you will need to provide an intelligent starting value to get the solver in the right ballpark for the solution you wish to see found. In the case of fzero, it can work with a single starting value, but a bracket that surrounds the solution can be better, helping the solver to converge more rapidly.
If you wish to solve for some other value than y==0, then just subtract that value from the function as fit. Now you are back at the problem fzero wants to solve, thus finding a zero.
Star Strider
2024-12-3,23:57
That generally goes something like this —
x = linspace(0, 10, 250);
y = sin(2*pi*x);
L = numel(x);
y_vals = 1-2*rand(4,1)
for k = 1:numel(y_vals)
zxi{k} = find(diff(sign(y - y_vals(k))));
end
zxi
for k1 = 1:numel(zxi)
% zxi{k1}
for k2 = 1:numel(zxi{k1})
idxrng = max(1,zxi{k1}(k2)-1) : min(zxi{k1}(k2)+1,L);
x_vals{k1,k2} = interp1(y(idxrng), x(idxrng), y_vals(k1));
end
end
x_vals
figure
plot(x, y)
hold on
for k = 1:size(x_vals,1)
xv = [x_vals{k,:}];
hp{k} = plot(xv, y_vals(k)*ones(size(xv)), 's', DisplayName="Y = "+y_vals(k));
end
hold off
grid
xlabel('X')
ylabel('Y')
legend([hp{:}], Location='best')
Make appropriate changes to use wiith your data.
.
4 个评论
Star Strider
2024-12-6,12:51
The interp1 function will throw a ‘Sample points must be unnique’ error, if you try to get more than one independent variable for each dependent variiable in a region. Finding the regions of the values that correspond to the desired value and interpolating only in that region prevents this, since in that region the values are unique.
There should be only one point corresponding to a desired value of the dependent variable in a specific region. If there are closely-related ponts, then it would be neceessary to run ‘zxi’ for each of them and do the interpolations separately.
x = linspace(0, pi);
y = sin(x);
L = numel(x)
zxi = find(diff(sign(y - 0.5)))
for k = 1:numel(zxi)
idxrng = max(1,zxi(k)-1) : min(zxi(k)+1,L)
get_x1(k) = interp1(y(idxrng), x(idxrng), 0.5);
end
get_x1
figure
plot(x, y, DisplayName='Data')
hold on
plot(get_x1, ones(size(get_x1))*0.5, 'rs', DisplayName='Desired Values')
hold off
grid
xlabel('X')
ylabel('Y')
title('Plot Demonstrating ‘Exact’ Intersections')
legend(Location='S')
get_x2 = interp1(y, x, 0.5) % Interpolating Both Points In A Single ‘interp1’ Call Fails
Walter Roberson
2024-12-4,9:26
Scattered X and y values with X increasing.
Suppose you had the input
X Y
1 1
1 0.5
1 -0.1
2 2
2 1
2 0.5
and you ask when y = 0
You would logically need to pair the (1,-0.1) with each of the X == 2 values and run interpolations on each of them:
(2,2)->(1,-0.1) ==> 22/11
(2,1)->(1,-0.1) ==> 12/11
(2,0.5)->(1,-0.1) ==> 7/6
and you would have to repeat this for any other locations where X = 1 was paired with a negative number.
Now if you extend the data with (3,0.1) then you also need to interpolate (1,-0.1) versus (3,0.1) . After all, the data is scattered, which implies that there might be missing entries for X == 2, so you need to consider the X = 1 to X = 3 pairs as well.
You can do this kind of calculation, but it is not exactly viable... unless you also list the X Y pairs that were taken into account in finding any particular crossing.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!