Using Matlab to get maximum values from many polyfitted curves in one figure - matlab

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
z1 = [1, 4, 9, 16, 25, 36, 49, 36, 25, 16, 8, 1];
z2 = [1, 5, 10, 17, 26, 38, 26, 17, 10, 9, 7, 1];
z3 = [1, 8, 18, 30, 40, 30, 18, 8, 7, 4, 3, 1];
plot(x, z1, '*');
hold on;
p1 = polyfit(x, z1, 5);
z1fit = polyval(p1, x);
plot(x, z1fit);
hold on;
plot(x, z2, '*');
p2 = polyfit(x, z2, 5);
z2fit = polyval(p2, x);
plot(x, z2fit);
hold on;
plot(x, z3,'*');
p3 = polyfit(x, z3, 5);
z3fit = polyval(p3, x);
plot(x, z3fit);
My question is, how to find every maximum value of each curve and display them? The maximum points are not the maximum values that I gave , It should be the peak points after fitting. I can simply read these peak points from the plotted figure, just want to know how to get the exact coordinate of the highest peak of the polynomial fit and return them. The degree of n (in my code 5) from polyfit is changeable.

To get the exact value of the maximum in the range of x, you can use the optimization toolbox.
x=[1,2,3,4,5,6,7,8,9,10,11,12];
z1=[1,4,9,16,25,36,49,36,25,16,8,1];
p1=polyfit(x, z1, 5);
fun = #(x) -polyval(p1,x);
x_max = fminbnd(fun,x(1),x(end))
figure; hold on;
plot(x,z1,'*');
fplot(#(x) polyval(p1,x),[x(1) x(end)],':');
plot(x_max,polyval(p1,x_max),'o');
legend('data','fit','max')
Use the polyfit function to get the coefficients of the fitted polynomial, use the polyval function to create a function handle to the polynomial you created and the fminbnd to "Find minimum of single-variable function on fixed interval". With the function handle, you can use the fplot function to plot the fit.
Edit:
If you want to calculate and plot the fitted curves in the same figure, you can:
x=[1,2,3,4,5,6,7,8,9,10,11,12];
z1 = [1, 4, 9, 16, 25, 36, 49, 36, 25, 16, 8, 1];
z2 = [1, 5, 10, 17, 26, 38, 26, 17, 10, 9, 7, 1];
z3 = [1, 8, 18, 30, 40, 30, 18, 8, 7, 4, 3, 1];
z = [z1;z2;z3];
figure; hold on;
for ii=1:size(z,1)
p1=polyfit(x, z(ii,:), 5);
fun = #(x) -polyval(p1,x);
x_max = fminbnd(fun,x(1),x(end))
plot(x,z(ii,:),'*');
fplot(#(x) polyval(p1,x),[x(1) x(end)],':');
plot(x_max,polyval(p1,x_max),'o');
end
The result, however, will not be that good, because you will have a lot of information in the same figure. Depending on what you want, that may be good. But I suggest you plot on different figures different data, maybe using subplots.

Related

Matlab: How to plot only the top portion of 3D bar plot

A fragment of my data is as follows.
nw, wl, F1
8, 8, 0.9229
8, 16, 0.9206
8, 32, 0.9312
16, 8, 0.907
16, 16, 0.9263
16, 32, 0.9141
32, 8, 0.9443
32, 16, 0.9255
32, 32, 0.9318
I am currently using Matlab 3D column bar plot to visualize the data. However, as the z values are mostly centered around 0.9, I would like to show only the top portion of the bars.
My code is as follows:
fid = fopen('testdata.csv', 'rt');
C = textscan(fid, '%s %s %f', 'Delimiter',',');
fclose(fid);
%# correctly reshape the data, and extract x/y labels
num = 6;
nw = reshape(C{1},num,[]); nw = nw(1,:); %x
wl = reshape(C{2},num,[]); wl = wl(:,1); %y
Z = reshape(C{3},num,[]);
%# plot 3D bars
bar3(Z)
xlabel('num walks'), ylabel('walk length'), zlabel('F1-Score')
set(gca, 'XTickLabel',nw, 'YTickLabel',wl, 'zlim', [85 100])
I tried to achieve this by using zlim in set(). However doing so results in none of the bars displaying.
Any help on this will be greatly appreciated.

Is Matlab's contourf function not using all provided data?

I am trying to plot an uncomplete data set using the contourf function. A minimum working example of this is
figure()
hold on;
scatter([1, 2, 3, 4, 5], [1, 1, 1, 1, 1])
scatter([1, 2, 3, 4, 5], [2, 2, 2, NaN, NaN])
scatter([1, 2, 3, 4, 5], [NaN, NaN, 3, 3, 3])
contourf([1, 2, 3, 4, 5],[1, 2, 3],[[10, 12, 20, 15, 11]; [8, 17, 7, NaN, NaN]; [NaN, NaN, 21, 19, 15]]);
hold off;
This produces the following figure:
What is the reason that the contour plot does not include all available data points? The result I would have expected would look something like this:
Is it possible to reach that result?

plotting a curve in MATLAB with prefefined x-axis

I have two vectors:
x = [1, 2, 3, 5, 6, 10, 20, 50, 100]
and
y=[7, 1, 2, 4, 2, 1, 5, 1, 1];
I am interested to plot Y as a function of X in MATLAB. So, this can be done by:
figure;
plot(x, y, '--rs');
In fact, the code above plots the curve, but the x-axis seems to be spaced as follows: 0, 10, 20, ..., 100.
What I want is to draw a curve which exhibits just the values in x as x-axis and their corresponding y-axis values. An example of x-axis is shown in the picture below.
Any help will be very appreciated!!
Instead of plotting using plot(x,y), use plot(1:numel(x),y) and use the XTick and XTickLabels` properties to change the labelling of the graph to suit your need.
Example:
clc
clear
x = [1, 2, 3, 5, 6, 10, 20, 50, 100];
y=[7, 1, 2, 4, 2, 1, 5, 1, 1];
plot(1:numel(x),y,'--rs')
set(gca, 'XTick', 1:length(x)); %// Change x-axis ticks
set(gca, 'XTickLabel', x); %// Change x-axis ticks labels.
Result:
Hope that helps!

How to plot only a fitting curve in MATLAB?

I want to graph a fitting curve given vectors with X and Y values, but also some example points, as the vectors are really big (10k+ terms).
Here is an equivalent MWE of the problem I'm facing:
xData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
yData = [1.5, 2.6, 3.7, 4.8, 5.9, 7.0, 8.1, 9.2, 10.3, 11.4];
[pX, pY] = prepareCurveData(xData, yData);
ft = 'linearinterp';
[fitresult, gof] = fit( pX, pY, ft, 'Normalize', 'on' );
gX = xData(1:2:end);
gY = yData(1:2:end);
hold on;
plot(fitresult, pX, pY);
plot(gX, gY, 'k*');
And here is the result of the MWE. As you can see, I can plot the selected points (in black), but the plot(fitresult, pX, pY); command also plots all the points I used to the curve fitting process (the small, blue ones):
I tried with the plot(fitresult); command but with that I lose the fitted curve, although the data points are also not plotted.
So, is there a way to plot a fitted curve without its data points?
I edited the code according to the discussion in the comment:
xData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
yData = [1.5, 2.6, 3.7, 4.8, 5.9, 7.0, 8.1, 9.2, 10.3, 11.4];
[pX, pY] = prepareCurveData(xData, yData);
ft = 'linearinterp';
[fitresult, gof] = fit( pX, pY, ft, 'Normalize', 'on' );
% set the scale for a new plot
gX = 1:20;
gY = fitresult(gX);
plot(gX, gY, 'r'); axis tight;

How to calculate the distance between points? [duplicate]

This question already has answers here:
Distance Between Two Points in Matlab
(3 answers)
Closed 9 years ago.
I want to calculate the distance between the user locations, access points and base station
figure('Color', 'white')
UserLocationX = randi(50, 1, 50);
UserLocationY = randi(50, 1, 50);
plot(UserLocationX, UserLocationY, '^-', 'MarkerSize', 5, 'LineWidth', 2), hold on
AccessPointX = randi(50, 1, 8);
AccessPointY = randi(50, 1, 8);
plot(AccessPointX, AccessPointY, 'go', 'MarkerSize', 5, 'LineWidth', 6), hold on
BaseStationX = 25;
BaseStationY = 25;
plot(BaseStationX, BaseStationY, 'rs', 'MarkerSize', 5, 'LineWidth', 6), hold on, grid on
leg = legend('User Location', 'Access Point', 'Base Station');
set(leg, 'Location', 'NorthEastOutside')
xlabel('x-candidate')
ylabel('y-candidate')
title('Scenario')
Use pdist2.
Between AP and users:
pdist2([AccessPointX(:) AccessPointY(:)],[UserLocationX(:) UserLocationY(:)])
Other cases are similar.