plotting a seperately defined function
Mostra commenti meno recenti
Dear community,
My problem is that i don't know how to plot a separately defined function, which is divided into different sections.
In the example below, it is obvious that MATLAB checks the condition only once and outputs the x-value +1 for all x-values.
The goal is that the function
in the interval [1,2) is defined by x+1
in the interval [2,3) is defined by x+2
in the interval [3,4) by x+3 etc.
The function described here is a minimal example.
I am appreciated for any help.
The plot is blue and the function as it should look is pink (excuse the inaccurate drawing, I hope it is clear what is meant).
x=1:0.001:8;
v=[0 1 2 3 4 5 6 7 8];
plot(x,test(x,v),'color',[.25 .75 1],'LineWidth',2)
%--------------------------------------------------------------
% seperate .m file
function y = test(time,value)
t = intervall(time,value) ;
y = time+value(t);
end
%-------------------------------------------------------------
% seperate .m file
function int = intervall(time,boundary)
j = 2;
while boundary(j) <= time
j=j+1;
end
int = j-1;
end

Risposte (1)
Walter Roberson
il 5 Feb 2021
function int = intervall(time,boundary)
int = interp1(boundary, time*(1+eps), 'next') - 1;
end
This depends upon the boundary values being in ascending order.
interp1(A,B,'next') finds the index of (each) B in A such that B <= A(index) -- so if B is not found exactly then it finds the "next" value in the matrix and gives back the index. If the boundaries were [0 1 2] and the input was exactly 1, the output of the interp1() would be 2, since 1 <= A(2) = 1. Likewise, input of 1.1 would give back index 3. since A(2) < 1.1 and 1.1 <= A(3)
But your original code scans until it finds a value that is after an equality and then steps back 1, whereas this code would stop at the equality. Your code would take the 1 and the 1.1 and move them both into the same bin. The work-around in this code to that is to take the inputs and nudge them slightly larger, to the next available floating point number, s that if they would have been an exact match, they are now in the next category, and then to back down by 1, exactly like in your code.
This code is vectorized, in contrast to your existing code. The problem with your existing code is that it was not vectorized but was being asked to handle vectors of values.
8 Commenti
Leon Budermüller
il 5 Feb 2021
Walter Roberson
il 5 Feb 2021
Ah, I forgot that 0*(1+eps) is 0, not eps(0). So where I wrote time*(1+eps) it should be time + eps(time)
Leon Budermüller
il 5 Feb 2021
Leon Budermüller
il 5 Feb 2021
Modificato: Leon Budermüller
il 5 Feb 2021
For this set of boundaries:
boundary = 1.5:5.5
Do you want the right endpoint of the boundary that contains the data?
interp1(boundary, 1.7, 'next')
Or do you want the index of that endpoint?
interp1(boundary, 1:numel(boundary), 1.7, 'next')
You can process a whole data set at once:
x = 1.5+(5.5-1.5)*rand(10, 1);
y = interp1(boundary, 1:numel(boundary), x, 'next');
You could discretize the data, though the bin numbers returned by discretize are the number of the bin that contains the data, not the index of the right edge of that bin. So it differs by 1 from the output of interp1.
value = discretize(x, boundary);
results = table(x, y, value, ...
'VariableNames', {'Original Data', 'Interpolated Data', 'Bin Number'})
Leon Budermüller
il 5 Feb 2021
boundary = [0,0.25,0.5,0.75,1,1.25,1.5,1.75,2];
time = [0,0.249999999,0.25];
int = interp1(boundary, 1:numel(boundary), time, 'previous')
Leon Budermüller
il 5 Feb 2021
Categorie
Scopri di più su Line Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!