How to pass values to a vector inside an anonymous function for ezplot?

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

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.
It's supposed to return the same length as th.
Why do you think th would be a 2D? It's just a cell array for each value of k i.e. 3 vectors for each loop over k.
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.
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;
@Walter Roberson Thanks.
@Guillaume Thanks, but th is a 1D vector. For each value of delta it's the same length of m which is a 1x100 vector. So why is it 2D?
Also, I have R2014b so I don't know what the third line says. How can this be 2D matrix and 1x100 vector is multiplied "kinda" element-wise with 1x3 vector?!
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?
Great!
Supposed to see three lines for each delta. But when I tried it with ezplot in a for loop, it showed me this error:
Error in ezplot>ezimplicit (line 257)
u = ezplotfeval(f, X, Y);
Error in ezplot (line 153)
hp = ezimplicit(cax, f{1}, vars, labels, args{:});
"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...
What I mean by three lines is that the equation x + y*th + k is satisfied.
Let's consider the simplest case when th is a scalar. Since th has the vector delta which has three values in it. It will produce three lines. I know that the output of ezplot is just 1x1 so when th is a scalar the ezplot will not complain.
The problem comes when we consider the th as a vector like I wrote in the code.
So for each value of delta which corresponds to the first vector of the th 2D matrix (as you explained) I would like to "sort of" convert this vector into a just symbolic 1x1 that ezplot output can produce. So actually, I don't know what myfun(x,y,k) should produce.
It happens that for a scalar value of th it produced this figure:
For that case:
>> myfun(1,1,1)
ans =
2
I hope I'm clear now and thanks.
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?

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 5 Mar 2017

Commented:

on 6 Mar 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!