I want to plot a function y=1-exp(c) ,where as the range of x is defined. The plot is to be between x and the function y. The plot just shows just 1 point instead of showing a series of points exponentially.I am new in Matlab.Sp,please help me where I am going wrong Here is the code:
for x = -10:0.25:10
if(x>0)
c=-6*x;
m=exp(c);
y = 1-m
end
plot(x,y,'o')
xlabel('x')
ylabel('y')
title('Plot')
end
This should do it:
x = -10:0.25:10; % define the x vector
c= -5*x.*(x>0); % using a logical condition the 'if' is avoided
y = 1-exp(c); % calc y given c
plot(x,y,'o')
xlabel('x')
ylabel('y')
title('Plot')
no 'for' loop or 'if' needed...
Your problem is the for loop. It is resetting the value of y and re-ploting that one point each loop. You don't need that loop at all. This code will do the trick for y = 1-exp(A*x)
Edit (2012-10-30) OP says y is zero for x<=0. #Nate's code in the answer above is probably best, but here I use logical indexing to show a different way to do the same thing.
x = -10:0.25:10; % <vector>
y = zeros(size(x)); % prealocate y with zeros and make it the same size as x
y(x>0) = 1 - exp(-5*x(x>0)); % only calculate y values for x>0
plot(x,y,'o')
xlabel('x')
ylabel('y')
title('Plot')
Related
I am stuck with an apparently simple problem. I have to revolve of 360° a 2D curve around an axis, to obtain a 3D plot. Say, I want to do it with this sine function:
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
surf(xx,yy,zz)
axis equal
I now want to visualize the numerical values of the Z plane, stored in a matrix. I would normally do it this way:
ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
If I visualize Z with
imagesc(Z)
I don't obtain the actual values of Z of the plot, but the "un-revolved" projection. I suspect that this is related to the way I generate the curve, and from the fact I don't have a function of the type
zz = f(xx,yy)
Is there any way I can obtain the grid values of xx and yy, as well as the values of zz at each gridpoint?
Thank you for your help.
Instead of bsxfun you can use meshgrid:
% The two parameters needed for the parametric equation
h = linspace(0,2) ;
th = 0:pi/20:2*pi ;
[R,T] = meshgrid(h,th) ;
% The parametric equation
% f(x) Rotation along Z
% ↓ ↓
X = sin(R) .* cos(T) ;
Y = sin(R) .* sin(T) ;
% Z = h
Z = R ;
surf(X,Y,Z,'EdgeColor',"none")
xlabel('X')
ylabel('Y')
zlabel('Z')
Which produce:
And if you want to extract the contour on the X plane (X = 0) you can use contour:
contour(Y,Z,X,[0,0])
Which produce:
I'm trying to plot a the eigenvectors of a 2D Dataset, for that I'm trying to use the quiver function in Matlab, here's what I've done so far :
% generating 2D data
clear ;
s = [2 2]
set = randn(200,1);
x = normrnd(s(1).*set,1)+3
y = normrnd(s(1).*set,1)+2
x_0 = mean(x)
y_0 = mean (y)
c = linspace(1,100,length(x)); % color
scatter(x,y,100,c,'filled')
xlabel('1st Feature : x')
ylabel('2nd Feature : y')
title('2D dataset')
grid on
% gettign the covariance matrix
covariance = cov([x,y])
% getting the eigenvalues and the eigenwert
[eigen_vector, eigen_values] = eig(covariance)
eigen_value_1 = eigen_values(1,1)
eigen_vector_1 =eigen_vector(:,1)
eigen_value_2 = eigen_values(2,2)
eigen_vector_2 =eigen_vector(:,2)
% ploting the eigenvectors !
hold on
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1))
My problem is the last line, I get the following error :
Error using quiver (line 44)
The size of Y must match the size of U or the number of rows of U.
It seems that I'm missing a size here but I can't figure out where!
thanks in advance for any hint
As the error says, X and Y parameters must have, respectively, the same size of U and V parameters. If you change the last part of your code:
% ploting the eigenvectors !
hold on
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1))
as follows:
x_0 = repmat(x_0,size(eigen_vector_2,1),1);
y_0 = repmat(x_0,size(eigen_vector_1,1),1);
% ploting the eigenvectors !
hold on;
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1));
hold off;
your script should properly work.
I'm wondering if anyone has any insight into why these two plot commands produce domains that are orders of magnitude different?
syms t
x1Axis = 0:.01:10;
fun1(t) = sin(t)
plot(sin(x1Axis))
hold on
y = sin(x1Axis)
plot(x1Axis, y)
fun1(t) is plotted "symbolically" while y is evaluated and plotted "discreetly". Should I be using a different plot method in the case of the first function?
No, you are not plotting the symbolic function correctly.
In your code, the instruction plot(sin(x1Axis)) is not a symbolic plot, but a numeric plot of the sine versus the index of each value.
From the plot documentation page:
plot(Y) creates a 2-D line plot of the data in Y versus the index of
each value.
If Y is a vector, then the x-axis scale ranges from 1 to length(Y).
To plot the symbolic function use fplot.
The following example will allow you to see that both the symbolic and numeric plots are the same:
xmin = 0;
xmax = 10;
% Symbolic Plot.
syms t
fun1(t) = sin(t);
fplot(fun1, [xmin xmax], '-r');
hold on;
% Numeric Plot.
x = xmin:.01:xmax;
y = sin(x);
plot(x, y, '--g');
% Add legend.
legend('Symbolic Plot', 'Numeric Plot', 'Location', 'north');
This is the result:
if the function F is available it is easy to draw surf plot i.e.
x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;
in my case I calculated F function using least square:
for example I have x and y vectors
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2754.5 4043.3 5376.3 5050.4];
the linear function of these two vector is define by
F= [1149.73 , 37.63];
therefore the estimation is equal to
z= [ones(5,1) x']*F';
which is
z = [1149.73 1519.67 4051.96 5589.35 4912.65];
and if it is plotted
plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');
The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs
[X,Y] = meshgrid(x,y);
but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this
Thanks
P.S: the F is calculated using pinv (least square).
You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors
xi=linspace(min(x),max(x),100)
yi=linspace(min(y),max(y),100)
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
contourf(XI,YI,ZI)
How do you plot a user-defined function of two variables in Matlab?
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
Z = my_func(X,Y);
surf(X,Y,Z);
Alternatively, if your function is not vectorized,
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
for x = 1:length(xs)
for y = 1:length(ys)
Z(x,y) = my_func(X(x,y), Y(x,y));
end
end
Z = my_func(X,Y);
surf(X,Y,Z);
ezsurf is a simple solution, or ezmesh, or ezcontour, or ezsurfc, or ezmeshc.
It has many types.
You can go plot gallery and select your variable and then types like mesh, 3D, surface, ...