This question already has answers here:
What is the advantage of linspace over the colon ":" operator?
(3 answers)
Closed 4 years ago.
I am wanting to do a very simple plot of a polynomial using the plot(x,y) function. Here is my full code
a = linspace(-3, 0.1, 3);
plot(a, a.^3 - 3*a - 2);
ax = gca;
c = ax.Color;
grid on;
Which outputs the following picture
Why doesn't the graph extend from -3 to 3 on the x-axis? Why does it stop just after $0$?
As the documentation states, in linspace(x1,x2,n) x1 is the begin value, x2 is the end value and n is the number of points. That's exacly what you see on your plot: 3 points: -3, 0.1 and one halfway (because of the linear spacing).
Since you want to have a particular spacing between your points, and not a certain number of points, I suggest you build your vector as:
a = -3:.1:3;
Related
This question already has answers here:
Spline interpolation in matlab in order to predict value
(2 answers)
Extracting x value given y threshold from polyfit plot (Matlab)
(1 answer)
Closed 4 years ago.
i have a very simple question but i can't find my wrong. I have two signals, one same x axes and two signals value on y axis. I need to find the y value of green signal on the black line, so where y value of blue signal is 50.05. It should be between 6-7. I thought that i should first get same x value (4.676). Then get the y value of green signal where x is 4.676. So i need to get first 4.676 but i can't get this x value on given y value. I tried this but it comes always empty matrix.
xvalue = interp1(x_bluesignal,y_bluesignal, 50.05)
and
idx = find(x_bluesignal == 50.05);
Xidx = x_bluesignal(idx);
Any idea? Thank you!
As #obchardon pointed out in the comments, you want to interpolate on the x-value, not the y-value. As a simple example, consider the following:
%Plot two original lines
x = linspace(0,20,101);
y1 = 5*x+2;
y2 = 3*x+5;
plot(x, y1, 'b', x, y2 ,'g'); hold on
%Plot intersection line at desired y_interest value
y_interest = 50.05;
xvalue = interp1(y1, x, y_interest);
plot([xvalue, xvalue], [0, 100], 'k--')
x_interest = interp1(x, y2, xvalue);
This produces the following plot:
Once you've interpolated on x to find the correct x-value, you can then feed that value to your next interpolation on the y-values of the green curve. In the example above, this will output 33.8300.
In both your attempts you are actually asking MATLAB for the y value where x is 50.05, rather than your intended query.
Both
xvalue = interp1(y_bluesignal,x_bluesignal, 50.05)
and
idx = find(y_bluesignal == 50.05);
Xidx = x_bluesignal(idx);
may work if there is an entered data point at y=50.05 (use a tolerance as in the above comments if this is a calculated value), the interp method will work regardless
This question already has an answer here:
Multiple colors in the same line
(1 answer)
Closed 4 years ago.
I am looking for a solution to this problem:
consider a function f (x) = 2x + 1, with x belonging to [0, 1000]. Draw the representative curve of f as a function of x, so that if ||f (x)|| <3 the representative curve of f is in red color and else represent the curve of f in blue color.
Help me because I am a new user of Matlab software
The code below should do the trick:
% Obtain an array with the desired values
y = myfunc(x);
% Get a list of indices to refer to values of y
% meeting your criteria (there are alternative ways
% to do it
indInAbs = find((abs(y)<3));
indOutAbs = find((abs(y)>=3));
% Create two arrays with y-values
% within the desired range
yInAbs = y(indInAbs);
xInAbs = x(indInAbs);
% Create two arrays with y-values
% outside the desired range
yOutAbs = y(indOutAbs);
xOutAbs = x(indOutAbs);
% Plot the values
figure(1);
hold on;
plot( xInAbs, yInAbs, 'r')
plot( xOutAbs, yOutAbs, 'b')
legend('in abs', 'out abs', 'location', 'best')
There are alternative ways to do it which could be more efficient and elegant. However, this is a quick and dirty solution.
Your threshold cannot be too low, otherwise it has not enough data to plot (if threshold=3) or cannot see the blue part. Here I use 500 such that you can see.
function plotSeparate
clc
close all
k=0
threshold=500
for x=1:0.5:1000
k=k+1
y=f(x)
if abs(y)<threshold
t(k,:)=[x,y];
else
s(k,:)=[x,y];
end
end
plot(s(:,1),s(:,2),'-r',t(:,1),t(:,2),'-b')
end
function y=f(x)
y=2*x+1;
end
This question already has answers here:
3D scatter plot with 4D data
(2 answers)
Closed 6 years ago.
Suppose I have a situation as follows:
clc; clear;
n = 1001;
m = 1000;
X = linspace(0,1,n);
Y = linspace(0,1,n);
randcolor = rand(m,3);
colorcode = randi(m,m,m);
For i = 1, ..., n and j = 1, ...,n, I would like to plot the points (X(i),Y(j))'s where the RBG color for (X(i),Y(j)) is randcolor(colorcode(i,j),:). I tried to do this the silly way: first declare
figure; hold on;
then do 2 nested loops, n steps each, and use plot to plot a single point n x n times:
for i = 1:n
for j = 1:n
plot(X(i),Y(j),'Marker','o',...
'MarkerEdgeColor',randcolor(colorcode(i,j),:),...
'MarkerFaceColor',randcolor(colorcode(i,j),:));
end
end
This technically worked but it was slow and MATLAB ate up all of my memory when n was increased. What's a better way to do this please?
p.s. In my actual problem, colorcode isn't actually randomly assigned. Rather, it's assigned based on some divergence criterion for a filled Julia set.
You want to use scatter instead of plot which allows you to specify the size and color of each point individually.
colors = rand(numel(X), 3);
S = scatter(X, Y, 100, colors);
This question already has answers here:
How to plot inequalities
(3 answers)
Closed 8 years ago.
I have a question about matlab. It seems simple but I can't find the solution for inequalities linear region plot in matlab. For example, I want to plot the regions of y-x, and y>x and show the color for each one. For any x, y, but we can assume x = [-50:50].
Thank you.
I tried this one but don't know how to show the color for the third parameter.
[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1);
ineq1 = Y<X;
ineq2 = Y>X;
ineq3 = Y>-X;
colors = zeros(size(X))+ineq1+ineq2+ineq3;
scatter(X(:),Y(:),3,colors(:),'filled')
[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1);
x1=reshape(X,[size(X,1)*size(X,2) 1]);
y1=reshape(Y,[length(x1) 1]);
a=[x1 y1];
ineq1=a(a(:,1)>a(:,2),:);
ineq2=a(a(:,1)<a(:,2),:);
ineq3=a(-a(:,1)<a(:,2),:);
scatter(ineq1(:,1),ineq1(:,2),3,'b','filled');
hold on;
scatter(ineq2(:,1),ineq2(:,2),3,'r','filled');
scatter(ineq3(:,1),ineq3(:,2),3,'g','filled');
I want to draw some plots in Matlab.
Details: For class 1, p(x|c1) is uniform for x between [2, 4] with the parameters a = 1 and b = 4. For class 2, p(x|c2) is exponential with parameter lambda = 1. Besides p(c1) = p(c2) = 0.5 I would like to draw a sketch of the two class densities multiplied by P(c1) and P(c2) respectively, as
a function of x, clearly showing the optimal decision boundary (or boundaries).
I have the solution for this problem, this is what the writer did (and I want to get), but there's no Matlab code, so I want to do it all by myself.
And this is what I drew.
And this is the MATLAB code I wrote.
x=0:1:8;
pc1 = 0.5;
px_given_c1 = exppdf(x,1);
px_given_c2 = unifpdf(x,2,4);
figure;
plot(x,px_given_c1,'g','linewidth',3);
hold on;
plot(x,px_given_c2,'r','linewidth',3);
axis([0 8 0 0.5]);
legend('P(x|c_1)','P(x|c_2)');
figure;
plot(x,px_given_c1.*pc1,'g','linewidth',3);
hold on;
plot(x,px_given_c2.*(1-pc1),'r','linewidth',3);
axis([0 8 0 0.5]);
legend('P(x|c_1)P(c_1)','P(x|c_2)P(c_2)');
As you can see, they are almost smiliar, but I am having problem with this uniform distribution, which is drawn in red. How can I change it?
You should probably change x=0:1:8; to something like x=0:1e-3:8; or even x=linspace(0,8,1000); to have finer plotting. This increases number of points in vectors (and therefore line segments) Matlab will use to plot.
Explanation: Matlab works with line segments when it does plotting!
By writing x=0:1:8; you create vector [0 1 2 3 4 5 6 7 8] that is of length 9, and by applying exppdf and unifpdf respectively you create two vectors of the same length derived from original vector. So basically you get vectors [exppdf(0) exppdf(1) ... exppdf(8)] and [unifpdf(0) unifpdf(1) ... unifpdf(8)].
When you issue plot command afterwards Matlab plots only line segments (8 of them in this case because there are 9 points):
from (x(1), px_given_c1(1)) to (x(2), px_given_c1(2)),
...
from (x(8), px_given_c1(8)) to (x(9), px_given_c1(9)).