Mathematica easily plots a function with constant value parameters over a range of different values for the constant(s).
With MATLAB, is this possible?
For instance: would MATLAB allow me to plot f(x)=x*e^a of different constant values of a?
Depends how you define easily:
>> alpha = [0.1 0.2 0.5 1];
>> x = 0:0.01:2;
>> plot(x,exp(x(:)*alpha))
>> grid on
>> legend(arrayfun(#(a)sprintf('alpha = %.2f',a),alpha,'uni',false))
fplot() can also be used to plot functions.
alpha = [0.1 0.5 1];
f = (#(x) exp(alpha.*x));
fplot(f, [0,2]); grid on; % Axis range = [0, 2]
legend('alpha=0.1', 'alpha=0.5', 'alpha=1.0');
And the output is:
Related
I want to plot a single histogram where some of the x-values are 0, while the other x-values range between 0.002 and 0.008. The problem is that even by specifying the xlim, the columns overlap but of course they should not.
x1 = zeros (3000,1);
hist(x1);xlim([0 0.008]);
h = findobj(gca,'Type','patch');
set(h,'Facecolor',[1 0 0]);
set(h,'FaceAlpha',0.7);
hold on
n = 3000;
R = [0.002 0.008];
x2 = rand(n,1)*range(R)+min(R);
hist(x2);xlim([0 1])
h1 = findobj(gca,'Type','patch');
set(h1,'facealpha',.3)
Do you really need to use two histograms?
hist([x1; x2])
I would like to create a density plot of this function:
In Maple, one could use the densityplot function to achieve this (code at the end), which gives:
However, I am not sure what to use for plotting a similar figure in MATLAB.
Here is my current MATLAB code:
x = [0:10:100];
y = [-50:10:50];
s = [10, 0];
i = [50,25];
for ii = 1 : length(x)
sir(ii) = -10 * 9.8 * log10((power((x(ii) - s(1)),2) + power((y(ii) - s(2)),2)) / (power((x(ii) - i(1)),2) + power((y(ii) - i(2)),2)));
end
Could someone suggest an equivalent in MATLAB?
For the density plot in Maple, I used
densityplot(sir(x,y), x=0..100, y=-50..50, axes=boxed, style=patchnogrid, scaletorange=-5..50, colorscheme = [black, "green", "white"])
You can use surf (a 3D surface plot) to achieve this, but you will need a finer grid than steps of 10 for it to look good!
Also you will need meshgrid to get all combinations of the x and y coordinates.
Please see the comments for further details.
% Set up grid points
x = 0:0.1:100;
y = -50:0.1:50;
[x,y] = meshgrid(x,y);
% Set up parameters i, s and g
i = [50 25]; s = [10 0]; g = 9.8;
% Work out density
% - no need for loop if we use element-wise operations ./ and .^
% - power(z,2) replaced by z.^2 (same function, more concise)
% - You forgot the sqare roots in your question's code, included using .^(1/2)
% - line continuation with "...", could remove and have on one line
sir = -10*g*log10( ((x-s(1)).^2 + (y-s(2)).^2).^(1/2) ./ ...
((x-i(1)).^2 + (y-i(2)).^2).^(1/2) );
% Plot, and set to a view from above
surf(x,y,sir,'edgecolor','none','facecolor','interp');
view(2);
% Change the colour scheme
colormap('bone')
Result:
Matching your example
You used the Maple command scaletorange=-5..50. This limits the scale between -5 and 50 (docs), so since sir is our scale variable, we should limit it the same. In MATLAB:
% Restrict sir to the range [-5,50]
sir = min(max(sir,-5),50);
% Of course we now have to replot
surf(x,y,sir,'edgecolor','none','facecolor','interp');
view(2);
Now, if you wanted the black/green colours, you can use a custom colormap, this would also smooth out the banding caused by the 'bone' colormap only having 64 colours.
% Define the three colours to interpolate between, and n interpolation points
black = [0 0 0]; green = [0 1 0]; white = [1 1 1];
n = 1000;
% Do colour interpolation, equivalent to Maple's 'colorscheme = [black, "green", "white"]'
% We need an nx3 matrix of colours (columns R,G,B), which we get using interp1
colormap(interp1(1:3, [black; green; white], linspace(1,3,n)));
With g=3.5 (not sure what you used), we get an almost identical plot
I have this function of 2 variables:
match = (2.*x-1).*y.^(.1) - 2.*x.*y;
Plot using ezplot(match, [0 1], [0 1]) yields:
I would like to shade (fill) the area under this green curve all the way to the 2 axis. I have tried to get the x and y data via get(h,'XData') and get(h,'YData'), and then use
area(x,y)
but the plot is not correct. Any suggestions?
Another option:
Assuming that you have a match() function:
function z = match(x,y)
z = (2.*x-1).*y.^(.1) - 2.*x.*y;
end
And you use ezplot():
fig = ezplot('match', [0 1], [0 1]);
The following code extract the data from the figure and plot the area:
h1 = findall(fig,'Type','hggroup');
matrix = get(h1,'ContourMatrix');
xData = matrix(1,3:end);
yData = matrix(2,3:end);
area(xData,yData)
xlim([0 1])
ylim([0 1])
The key point is that ezplot() uses a contour object, therefore it is a little more trickier to get the data.
OK, a rather inefficient solution is by using the symbolic toolbox:
syms y
match = #(x) (2.*x-1).*y.^(.1) - 2.*x.*y;
n = 100;
xs = linspace(0,1,n);
yval = zeros(n,2);
for ii=1:n
x=xs(ii);
temp = solve(match(x),'y', 'Real', true);
if length(temp)==1
yval(ii,1) = temp;
elseif length(temp)==2
yval(ii,:) = temp;
end
end
area(xs,yval(2))
Mind that the first solution is always '0'. I don't know if performance is an issue but this might be working for you!
I want to add markers at the limits of a function in matlab. I am plotting the function using fplot this is what I tried:
user_func = '2*x-3';
user_limits = '-2,2';
user_limits = regexp(user_limits, '\,', 'split');
user_limit(1) = str2num(user_limits{1});
user_limit(2) = str2num(user_limits{2});
h = fplot(func,limits);
I am trying to add markers at the limits only (size 10 color 'r'). any idea on how to do that?
thank you
Not sure if this is exactly what you are trying to accomplish but I modified your code slightly so I could plot the function (using an anonymous function):
user_func = #(x) 2*x-3;
user_limits = '-2,2';
user_limits = regexp(user_limits, '\,', 'split')
user_limit(1) = str2num(user_limits{1})
user_limit(2) = str2num(user_limits{2})
figure;fplot(user_func,[user_limit(1) user_limit(2)]);
Next, set the ticks at your locations and change the font size to 10 pt:
set(gca,'XTick',[user_limit(1) user_limit(2)],'FontSize',10);
Change the color of your labels to red:
set(gca, 'XColor', [1 0 0]);
set(gca, 'YColor', [1 0 0]);
Just so you can see the ticks, stretch the x-range a bit:
axis([-2.1 2.1 0 1]); axis 'auto y'
EDIT: After some additional input from the OP, the red tick markers can be plotted as shown below.
First let the x-position at the first limit be given by:
x1 = user_limit(1);
The y-value for first marker is then obtained from the anonymous function like this:
y1 = user_func(x1);
y2 = y1;
We have, y2 = y1, since you want the y-value where where your function first crosses the x-axis to be the same. Now make your plot like this (with x2 = user_limit(2)):
hold on;
plot(x1, y1, 'ro', x2, y2,'ro');
hold off;
giving a plot like:
I'm trying to represent a the intersection of two fuzzy sets as a 3d mesh in MatLab.
Here are my sets of vectors:
x = [0.3 0.5 0.7]
y = [0.5 0.7 0.1]
Followed by these statements:
[u,v] = meshgrid(x,y)
w = min(u,v)
mesh(u,v,w)
The x and y ticks seem to be all over the place and do not correlate to the actual index number of each vector i.e. 1 to 3, and the graph should represent the shape of a small triangle/T-norm.
At the moment it looks like this:
Here is an example out of my book I'm following:
Ignore what looks like fractions, they are delimiters. Here is the resulting graph:
After looking up fuzzy sets and intersections, here's what I've come up with. First, let's reproduce the textbook example:
% possible values and associated degrees of truth for F
Fv = 1 : 5;
Ft = [0 0.5 1 0.5 0];
% possible values and associated degrees of truth for D
Dv = 2 : 4;
Dt = [0 1 0];
% determine degrees of truth for fuzzy intersection
It = bsxfun(#min, Ft', Dt);
% plot
h = mesh(Dv, Fv, It);
set(h, 'FaceColor', 'none')
set(h, 'EdgeColor', 'k')
xlim([0 4.5])
ylim([0 5])
xlabel D
ylabel F
view(37.5, 30)
The result is:
Not as pretty as in your book, but the same thing.
Applying the same code to your example yields:
Via the arguments u,v you are telling mesh to use the values in them, i.e. the values from x and y, for the positioning of the data points and corresponding ticks. If you just want positions and ticks at 1, 2, 3, leave these arguments out.
mesh(w)