ezplot in MATLAB, how to plot using a function handle? - matlab

I've tried this:
linefunca = #(xa,ya) aa*xa + ba*ya + ca;
figure(1)
imshow(Pica);
hold on;
ezplot(linefunca,[1,1072,1,712]);
But I'm returned with this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ezplotfeval/applyfun (line 80)
z(i) = feval(f,x(i),y(i));
Error in ezplotfeval (line 65)
z = applyfun(x,y);
Error in ezplot>ezimplicit (line 257)
u = ezplotfeval(f, X, Y);
Error in ezplot (line 153)
hp = ezimplicit(cax, f{1}, vars, labels, args{:});
Error in ps3 (line 313)
ezplot(linefunca,[1,1072,1,712]);
aa,ba,ca are all known values (column vectors). The x and y limits are the size of the image that I'm working with. I'm trying to plot a set of epipolar lines. Any suggestions?
EDIT:
lt = length(aa);
linefunca = #(x,y,t) aa.*x(t) + ba.*y(t) + ca(t);
figure(1)
imshow(Pica);
hold on;
for t=1:lt
ezplot(#(x,y,t) linefunca(x,y,t),[1,lt]);
end

As far as I know, ezplot can not plot a series of lines like plot. A way to work around this would be to add a parameter k to the anonymous function, which is used to select the current line. You can then go through all lines in a for loop and plot them one-by-one.
Further: as it is stated on the ezplot help page, you have to use the array functions .*, ./ and .^ , so ezplot can use vectors to evaluate the function.
N = 5;
aa = rand(N,1); ba = rand(N,1); ca = rand(N,1);
linefunca = #(xa,ya,k) aa(k).*xa + ba(k).*ya + ca(k);
hold on
for k=1:N
ezplot(#(x,y)linefunca(x,y,k),[-5,5,-5,5]);
end
hold off

Related

Plotting logistic map

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

Plot square root in a contour

I have two variables x and y. I decide to plot the square root of their difference using contour as follows:
x=0:0.1:100;
y=0:0.1:100;
G=sqrt(x-y);
test2 = G;
test2(~(G<0)) = nan;
[C,h]=contourf(x,y,G,'ShowText','off');
set(gca,'FontSize',20)
However I get this error : Error using contourf (line 69)
Z must be size 2x2 or greater.
If that is resolved, I want to reach my goal and plot the actual function which relies on x, y and G itself as follows:
Function = 2 sqrt(x) / G * acoth((sqrt(x) + y/2 )/G )
I give an example
x=0:0.1:100;
y=0:0.1:100;
[X, Y]=meshgrid(x,y);
G=sqrt(X-Y);
test2 = G;
test2(~(G<0)) = nan;
[C,h]=contourf(X,Y,abs(G),'ShowText','off');
set(gca,'FontSize',20)
Input for contour should be 2D array, but your arrays are 1D.
Here, G is complex number. When you plot G, you should plot absolute G.
The result will be like below.
Regarding your function,
H=((2*sqrt(X))./G).*acoth((sqrt(X) + Y/2 )./G );

plotting scatter3 and surf plots from loop in matlab

I want to plot scatter3 and surf plots from a loop. Below is my code but it isn't working...not sure where I'm going wrong but clearly something is wrong with the z matrix?
for e = 1:10;
x = rand(1,3);
y = rand(1,3);
A = x+y;
subplot(2,2,1)
p = find(A(:,1) > 1.1 & A(:,1) < 1.6);
Result = A(p,:);
scatter3(Result(:,1), Result(:,2), Result(:,3))
hold on
z(e,:) = [Result(1) Result(2) Result(3)];
end
subplot(2,2,2)
surf(z)
I will reiterate what I said in my comment to you. I got this error message when trying to run your code: Attempted to access Result(1); index out of bounds because numel(Result)=0. This is because your p condition isn't satisfied - MATLAB could not find any elements in the first column that are between 1.1 and 1.6.
As such, what I would suggest you do is check to see if Result is empty before trying to access the value itself. However, I would suggest you don't write a loop and generate all of the random values at once, then do the filtering with the Boolean conditions. Therefore, the equivalent code without using a loop would be this:
x = rand(10,3);
y = rand(10,3);
A = x+y;
p = A(:,1) > 1.1 & A(:,1) < 1.6;
z = A(p,:);
figure;
subplot(2,1,1);
scatter3(z(:,1), z(:,2), z(:,3));
subplot(2,1,2);
surf(z);
We generate 10 3D points for x and y at the beginning, then add these and store this into A. Next, we find the rows in A that are between 1.1 and 1.6 in the first column and store this as a logical array. We then use this array to index into A and store the results into z. This is the recommended approach if you want to extract certain elements into an array rather than using find.
Once we obtain z, we plot these points with scatter, then also find a surface plot with surf for the same matrix. BTW, I've fixed your subplot as you are only creating two plots, yet you are allocating space for 4 plots.
If you're absolutely bent on using your code, you would simply do this:
z = []; %// Change
for e = 1:10
x = rand(1,3);
y = rand(1,3);
A = x+y;
subplot(2,1,1)
p = find(A(:,1) > 1.1 & A(:,1) < 1.6);
Result = A(p,:);
scatter3(Result(:,1), Result(:,2), Result(:,3))
hold on
if ~isempty(Result) %// Change here
z = [z; Result(1) Result(2) Result(3)]; %// Change
end
end
subplot(2,1,2)
surf(z)
What's important is the initialization of z. I made this empty, and we only add to z if Result is not empty - this will happen if you generate a number that is not between 1.1 and 1.6.

Drawing a line in matlab plot

I am beginner in matlab programming, so i wrote this little programm to see it in action, and now I have a little problem because I am not sure why it is not working.
x = zeros(50);
squared = zeros(50);
cubed = zeros(50);
for num = 1:50
x(num) = num;
squared(num) = num^2;
cubed(num) = num^3;
end
% calculate the mean
mean_cubed = mean(cubed);
% clear screen and hold the plot
clf;
hold on
plot(x, squared);
plot(x, cubed);
plot([0, 50], [mean_cubed, mean_cubed]);
hold off
The main program is when i start the program i get a error:
Error using plot
Vectors must be the same lengths.
Error in basic_mathlab_plotting_2 (line 20)
plot([0, limit], [mean_cubed, mean_cubed]);
I think the size of vector are the same, so i dont know what is wrong.
Thanks!!!
In the first lines, you probably meant
x = zeros(1,50);
squared = zeros(1,50);
cubed = zeros(1,50);
Note that zeros(50) is equivalent to zeros(50,50) and so it returns a 50x50 matrix.
In addition, those lines and the for loop could be replaced by
x = 1:50;
squared = x.^2;
cubed = x.^3;
This applies the important concept of vectorization, by using the element-wise power operation.

Matlab error , matrix dimensions dont agree

im trying to plot 2 figures, using fplot and plot functions, but for my plot (fig2) , i get an error and don't understand why;
Error using /
Matrix dimensions must agree.
Error in bhpfilter (line 9)
H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);
Error in #(f)bhpfilter(f,fo,g)
function [H] = bhpfilter(f, fo, g)
%freq finds the filter frequency response in V/V
%fo is the cut off frequency, f is the input frequency and g is the filter
%gain
if fo <= 0 || g <=0 %error checking
error('Inputs invalid');
else
H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);
end
fo=1200.;
g=2.;
H =#(f) bhpfilter(f,fo,g);
H_1 = #(f) bhpfilter (f,fo,g)-0.8;
figure (1);
fplot(H,[0 2000]);
title('Plot of H vs f using fplot');
xlabel('Frequency (Hz)');
ylabel('Filter frequency response (V/V)');
fprintf('The value of f that gives a response of 0.8 is %f Hz\n',fzero (H_1, [0 2000])); %placed this line of code here so that it can be visible in command window , showing it works
figure (2);
plot([0:2000],H([0:2000])); % code will find individual values of H(1), H(2) etc.. but will not find H([0:200])
title('Plot of H vs f using plot');
xlabel('Frequency (Hz)');
ylabel('Filter frequency response (V/V)');
In the line H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3); g and fo are scalars while f is a vector. For division MATLAB doesn't recognize the divide operator as element by element division when it is scalar/vector (it does for the other way around). You have to put:
H = 3*g ./ ( (fo./f).^2 + 3*(fo./f)+3);
Hope that helps.