Effacer les filtres
Effacer les filtres

Best method to define a linear piece-wise function given limits and line equations?

20 vues (au cours des 30 derniers jours)
I am writing a function(also new to matlab) that is fed the following:
1. the x limits of each piece
2. the slope and intercept values of each piece
3. the y values for which I am trying to find corresponding x values
How can I write a function that can take any number of pieces, and create a piece-wise function? I then would like to be able to feed it y values and return the corresponding x values. I currently can only write the function for a given number of pieces, not any number..?
My code thus far:
%%define the piecewise function
function x = peicewisehot(y,Hbreaks,slope)
%y; input from user
%Hbreaks; vertical breaks
%slope; slope of equation
%x = (y-b)/m
for i = 1:length(y)
if y(i) >= Hbreaks(1) & y(i) < Hbreaks(2)
x(i) = (y(i) - b(1))./slope(1);
elseif y(i) >= Hbreaks(2) & y(i) < Hbreaks(3)
x(i) = (y(i) - b(2))./slope(2);
else
x(i) = nan;
end
end

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Jan 2012
  3 commentaires
Walter Roberson
Walter Roberson le 3 Jan 2012
yinterp1 = interp1(B, Hhotpt, 'linear', 'pp');
Notice the "u" is not passed, as you are not interpolating at specific points when you create the pp.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by