How to pass values to a vector inside an anonymous function for ezplot?
Show older comments
I wonder how can I benefit from passing additional parameter for ezplot function.
I'm trying to plot implicit function that has a vector th inside which has the problem.
Here is the code:
delta = pi*[1 2 3];
m = 1:100;
th{1,1}(1:100) = 2*pi*m*delta(1);
th{2,1}(1:100) = 2*pi*m*delta(2);
th{3,1}(1:100) = 2*pi*m*delta(3);
myfun = @(x,y,k) x + y.*th{k,1}(1:100) + k;
for k = 1:3
ezplot(@(x,y)myfun(x,y,k));
hold on
end
10 Comments
Guillaume
on 5 Mar 2017
What is myfun supposed to return for a particular x, y and k? At the moment it returns a vector of 100 elements. I don't see how you could plot that.
Also, in your example, th would be much better as a 2D matrix rather than a cell array of vectors.
Ezz El-din Abdullah
on 5 Mar 2017
Walter Roberson
on 5 Mar 2017
th(1,1:100) = 2*pi*m*delta(1);
th(2,1:100) = 2*pi*m*delta(2);
th(3,1:100) = 2*pi*m*delta(3);
myfun = @(x,y,k) x + y.*th(k,1:100) + k;
This would be more efficient (faster) and use less memory.
Guillaume
on 5 Mar 2017
Yes, as I said it returns a vector of 100 elements (the length of th) for a given x and y. So what is ezplot supposed to plot if for each x and y you've got 100 values?
th as a 2D matrix would simplify the code you've written to:
delta = pi * [1; 2; 3]; %column vector
m = 1:100; %row vector
th = 2*pi*m.*delta; %create th as a 2D matrix, R2016b only
%th = bsxfun(@times, 2*pi*m, delta); %%create th as a 2D matrix, R2016a and earlier
myfun = @(x, y, k) x + y.* th(k, :) + k;
Ezz El-din Abdullah
on 5 Mar 2017
Guillaume
on 5 Mar 2017
Walter and I are saying the same thing and producing the same th. th as a 2D matrix is a lot easier. As per the comments I wrote, on R2014b, you'd use:
delta = pi * [1; 2; 3]; %column vector
m = 1:100; %row vector
th = bsxfun(@times, 2*pi*m, delta);
myfun = @(x, y, k) x + y.* th(k, :) + k;
Each row of th is your original 1D vector that you put in the cells of the cell array.
You still haven't answered the question of what it is you want to plot. What should be plotted at (x,y) = (1,1), for example, when the function returns 100 different values at that point?
Ezz El-din Abdullah
on 6 Mar 2017
Edited: Ezz El-din Abdullah
on 6 Mar 2017
Guillaume
on 6 Mar 2017
"Supposed to see three lines for each delta" What three lines?
The problem is still the same, ezplot is complaining since for each x,y coordinate where it's trying to plot, you're giving it 100 values. It doesn't know what to with these 100 values (and neither do I), it only wants one.
So, once again, for k=1, what should be plotted at the point(x,y) = (1,1), when myfun returns 100 values:
>>x = 1, y = 1, k = 1;
>>myfun(x, y, k)
ans =
21.739 41.478 61.218 80.957 100.7 120.44 140.17 159.91 179.65 ....
Similarly, what should be plotted at (x,y) = (2,2), and so on...
Ezz El-din Abdullah
on 6 Mar 2017
Guillaume
on 6 Mar 2017
I understand everything you wrote except the I would like to "sort of" convert this vector into a just symbolic 1x1 that ezplot output can produce. It doesn't sound that you actually know what you want.
ezplot plots where myfun(x, y) == 0. What does equal to 0 mean when myfun(x, y) is a vector of 100 values? Do you want the zeros of the function
f(x, y) = x + a*y + k
for 100 different scalar a and 3 different scalar k, resulting in 300 plots?
Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!