In MATLAB, if I want to plot density of a variable V I have to do
[x, y] = ksdensity(V);
plot (y, x);
If I do plot(ksdensity(V)), it only plots x and not x Vs y.
Is there an easier alternative to give ksdensity() as an argument to plot() and do the same job as plot(y, x)?
You can refactor it into a function that takes in V and plots y vs x:
function h = plot_ksdensity(V, varargin)
[x, y] = ksdensity(V);
h = plot (y, x, varargin{:});
end
using varargin means you will still have access to plot options like colours. hold on will also still work because this just calls the regular plot function.
Unfortunately no. If you don't specify explicitly the outputs, a function will return always the leftmost one from output parameter list. To convince yourself about that, create the function ftest() somewhere in your MATLAB path:
function [x, y] = ftest( )
x = 1;
y = 2;
end
then call it in the Command Window without specifying the outputs
>> ftest()
ans =
1
Related
This is my code:
syms x y;
f= x^2/(y-y^2);
ezcontour(f,[-1,1],[0.1,0.9]);
How can i show the labels? I wan to show something like this:
Thank You very much!
Using contour :
x = [-1:0.01:1];
y = [0.1:0.01:0.9];
[X, Y] = meshgrid(x,y);
f= X.^2./(Y-Y.^2);
[C, h] = contour(f);
clabel(C, h);
clabel wants as an input the contour matrix being displayed by the Contour object. While ezcontour doesn't return the matrix like contour does, the Contour object has a 'ContourMatrix' property. If you specify an output for ezcontour it will return the handle to the plotted contour that can be queried directly.
For example:
f = #(x, y) x.^2/(y-y.^2);
h = ezcontour(f, [-1, 1], [0.1, 0.9]);
C = h.ContourMatrix; % R2014b or newer
% C = get(h, 'ContourMatrix'); % R2014a and older
clabel(C, h);
Returns the desired output:
Alternatively, you can can just pass the handle to the contour to obtain the same result:
clabel([], h);
Per the documentation:
If you do not have the contour matrix C, then replace C with [].
I am currently trying to plot f(x) = r*x*(1-x) (where r =3) and y=x on the same graph by using:
syms r x;
f = symfun(r*x*(1-x), x)
r = 3
plot(f,x)
plot(x,x)
But my code keeps resulting in the error:
Error using plot
A numeric or double convertible argument is expected
Please can someone help point out where I am going wrong.
The error's pretty clear: pass a numeric argument to plot. You're feeding it a symbolic function. Just use
r = 3;
x = 0:0.1:10; %// set some x
f = (r.*x.*(1-x)); %// dots make the multiplication element-wise
figure; %// opens figure
hold on %// plots multiple things in one figure
plot(f,x)
plot(x,x,'r') %// produces a red plot
I'm trying to create a function, that has two output arguments:
1. The calculated f(x) value
2. The gradient
But it's calling itself recursively all the time.
What am I doing wrong?
function [y, gra] = f1(x)
y = x^2
syms z
gra = gradient(f1(z))
Thanks.
edit:
Now I have this:
function [y, gra] = f1(x)
y = x^2
if nargout == 2
syms x
gra = gradient(f1(x))
end
edit 2:
I'm trying to use the function in the following:
[y, grad] = f1(5);
y_derived = grad(10);
I think this is what you want to do:
function [y, gra] = f1(x)
f=#(x) x^2;
y=f(x); %// calculate y
syms z %// initialise symbolic variable
gra=gradient(f(z),z); %// symbolic differentiation
This will return g as a symbolic function. To calculate a value, you can use subs(gra,z,123), or, if you are going to evaluate it many times, do gradFunc=matlabFunction(gra) then gradFunc(v) where v is a vector or matrix of points you want to evaluate.
That's because the argument into gradient is your function name f1(z). As such, it keeps calling f1 when your original function is also called f1, and so the function keeps calling itself until you hit a recursion limit.
I think you meant to put gradient(y) instead. Try replacing your gradient call so that it is doing:
gra = gradient(y);
Why use 'fplot' to plot functions when we can use 'plot'? I can't understand
With plot you have to manually define the x values and compute the corresponding y given by the function.
>> x = 0:.01:1;
>> y = sin(10*x);
>> plot(x,y,'.-')
With fplot you define the function generically, for example as an anonymous function; pass a handle to that function; and let Matlab choose the x values and compute the y values. As an example, take a difficult function:
>> f = #(x) sin(1/x);
Assume we want to plot that between 0.01 and 1:
>> lims = [.01 1];
>> fplot(f, lims, '.-')
See how Matlab does a pretty good job choosing closer x values in the left area, where the function becomes wilder.
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, ...