how to use conditional ezplot ?

m=0;
nc=1.0;
na=2.0;
nb=1.0;
n=2;
lambda0=1.50e-6;
k0=2*pi/lambda0;
rc=5/k0;
a=1/k0; %0.211e-6;
b=1/k0; %0.305e-6;
%omega=rc core radius%
%W=beta/k;
%K=k/k0;
kc=@(K,W)k0*K*sqrt((nc)^2-(W)^2);
ka=@(K,W)k0*K*sqrt((na)^2-(W)^2);
kb=@(K,W)k0*K*sqrt((nb)^2-(W)^2);
t=@(K,W)(kc(K,W)*rc);
%For TE
xs=@(K,W)(cos(kb(K,W)*b)-(i/2)*((kb(K,W)/ka(K,W))+(ka(K,W)/kb(K,W)))*sin(kb(K,W)*b))*exp(-i*ka(K,W)*a);
ys=@(K,W)((i/2)*((kb(K,W)/ka(K,W))-(ka(K,W)/kb(K,W)))*sin(kb(K,W)*b))*exp(i*ka(K,W)*a);
xxs=@(K,W)real(xs(K,W));
xxxs=@(K,W)abs(xxs(K,W));
if xxxs<1;
Ritesh=@(K,W)(((-besselj(m+1,t(K,W))+(m/t(K,W))*besselj(m,t(K,W)))/besselj(m,t(K,W)))+((kc(K,W)*(((xxxxs(K,W))+sqrt((xxxxs(K,W))^2-1))^(n-1)-xs(K,W)-ys(K,W)))/(i*ka(K,W)*(((xxxxs(K,W))+sqrt((xxxxs(K,W))^2-1))^(n-1)-xs(K,W)+ys(K,W)))));
h1 = ezplot(Ritesh,[0.0*10^6/k0 31.4*10^6/k0 0 1]);
%h1 = ezplot(Ritesh,[0.0*10^6/k0 31.4*10^6/k0 0.008032 0.98394]);
set(h1,'Color','black','LineWidth',2);
else
end

Réponses (1)

Walter Roberson
Walter Roberson le 30 Mai 2016

0 votes

Your xxxs is a function handle to a function that requires two arguments, but you are trying to use
if xxxs<1
which tests the function handle itself instead of applying the function handle to any arguments.
Are you trying to program it so that the formula in Ritesh applies in some parts of the ezplot but not in others? If so then what value should apply in the other parts? It is not possible to have ezplot apply only to some places -- but it is possible to create formula whose value is NaN in some places and meaningful values in others.

6 commentaires

Ritesh Chaurasia
Ritesh Chaurasia le 31 Mai 2016
Dear Sir, the function handler xxxs is the absolute value of xxs and i have a condition that xxxs<1 for which i have to use ezplot to calculate the given function. I have too take only those solution throgh ezplot for which xxxs<1. so kindly suggest me how can i use conditional ezplot in same syntex of ezplot.
Walter Roberson
Walter Roberson le 31 Mai 2016
Modifié(e) : Walter Roberson le 31 Mai 2016
That cannot be done. ezplot requires that the function value be defined for all pairs of values in the ranges you give. The defined value can be 0 or one of the infinities or nan, but it has to be some value. You are attempting to have the value not even be defined outside of a range.
Ritesh Chaurasia
Ritesh Chaurasia le 31 Mai 2016
I know the limit of ezplot...But i am asking for any technique to use condition in ezplot to obtain desired solution. actually if you will see the function deeply,you will find that xxxs is the part of the function whose value should be more than 1 but i have to find the solution only for xxxs<1. Is there any solution for this problem??
No, as long as you use ezplot() you need to use a function that has some value for all values in the x and y range.
Your code was not vectorized; I did that for you.
Your code used xxxxs but did not define it. In the below, I guessed that it might be the same as xxxs. You should adjust that at need.
In the below, the Ricond function uses a little bit of a trick to force NaN or inf to be assigned in places that are out of bounds. The code does that by dividing by the result of the logical test. The logical test is 1 when the location is in-bounds, which does not change the answer. The logical test is 0 when the location is out of bounds, which causes a division by 0, which gives +inf or -inf or nan, none of which will be plotted.
ezplot does not sample the grid finely enough to see any results, so I switched to surf.
If you increase the refine past about 150, you will see a spike up to about 10000 that washes out everything else. If you rotate the surface you can see that there are a bunch of spikes; there is a pattern to them.
m = 0;
nc = 1.0;
na = 2.0;
nb = 1.0;
n = 2;
lambda0 = 1.50e-6;
k0 = 2.*pi./lambda0;
rc = 5./k0;
a = 1./k0; %0.211e-6;
b = 1./k0; %0.305e-6;
%omega=rc core radius%
%W = beta./k;
%K = k./k0;
kc = @(K,W) k0.*K.*sqrt((nc).^2-(W).^2);
ka = @(K,W) k0.*K.*sqrt((na).^2-(W).^2);
kb = @(K,W) k0.*K.*sqrt((nb).^2-(W).^2);
t = @(K,W) (kc(K,W).*rc);
%For TE
xs = @(K,W) (cos(kb(K,W).*b)-(i/2).*((kb(K,W)./ka(K,W))+(ka(K,W)./kb(K,W))).*sin(kb(K,W).*b)).*exp(-i.*ka(K,W).*a);
ys = @(K,W) ((i/2).*((kb(K,W)./ka(K,W))-(ka(K,W)./kb(K,W))).*sin(kb(K,W).*b)).*exp(i.*ka(K,W).*a);
xxs = @(K,W) real(xs(K,W));
xxxs = @(K,W) abs(xxs(K,W));
xxxxs = xxxs; %GUESS
Ritesh = @(K,W) (((-besselj(m+1,t(K,W))+(m./t(K,W)).*besselj(m,t(K,W)))./besselj(m,t(K,W)))+((kc(K,W).*(((xxxxs(K,W))+sqrt((xxxxs(K,W)).^2-1)).^(n-1)-xs(K,W)-ys(K,W)))./(i.*ka(K,W).*(((xxxxs(K,W))+sqrt((xxxxs(K,W)).^2-1)).^(n-1)-xs(K,W)+ys(K,W)))));
Ricond = @(K, W) Ritesh(K,W) ./ (xxxs(K,W) < 1);
refine = 75;
K_ = linspace(0.0*10^6/k0, 31.4*10^6/k0, refine);
W_ = linspace(0, 1, refine);
[KK, WW] = ndgrid(K_, W_);
Zric = Ricond(KK, WW);
surf(KK,WW,real(ZRic), 'edgecolor','none');
The imaginary part is small and probably mostly numeric round off. But right along W = 0 it might not be negligible.
Ritesh Chaurasia
Ritesh Chaurasia le 7 Juin 2016
Modifié(e) : Walter Roberson le 7 Juin 2016
I am thankful, You did effort for me. I have used your redeveloped mat programme... But i faced problem in using conditional statement with surfplot also as in ezplot.
Kindly, I request you first understand my problem and give me solution accordingly.the new program-me is fallowing-
%%%%%%%%%%%%%%%%%%%%%%%%
close all
clear all
clc
m = 0;
nc = 1.0;
na = 2.0;
nb = 1.0;
n = 2;
lambda0 = 1.50e-6;
k0 = 2.*pi./lambda0;
rc = 1./k0;
a = 1./k0; %0.211e-6;
b = 1./k0; %0.305e-6;
%omega=rc core radius%
%W = beta./k;
%K = k./k0;
refine = 375;
K = linspace(0.0*10^6/k0, 41.4*10^6/k0, refine);
W = linspace(0, 1, refine);
kc = @(K,W) k0.*K.*sqrt((nc).^2-(W).^2);
ka = @(K,W) k0.*K.*sqrt((na).^2-(W).^2);
kb = @(K,W) k0.*K.*sqrt((nb).^2-(W).^2);
t = @(K,W) (kc(K,W).*rc);
%For TE
xs = @(K,W) (cos(kb(K,W).*b)-(i/2).*((kb(K,W)./ka(K,W))+(ka(K,W)./kb(K,W))).*sin(kb(K,W).*b)).*exp(-i.*ka(K,W).*a);
ys = @(K,W) ((i/2).*((kb(K,W)./ka(K,W))-(ka(K,W)./kb(K,W))).*sin(kb(K,W).*b)).*exp(i.*ka(K,W).*a);
xxs = @(K,W) real(xs(K,W));
xxxs = @(K,W) abs(xxs(K,W));
xxxxs= xxxs(K,W);
if xxxxs < 1
xxxxs1=xxxxs;
Ritesh = @(K,W) (((-besselj(m+1,t(K,W))+(m./t(K,W)).*besselj(m,t(K,W)))./besselj(m,t(K,W)))+((kc(K,W).*(((xxxxs1(K,W))+sqrt((xxxxs1(K,W)).^2-1)).^(n-1)-xs(K,W)-ys(K,W)))./(i.*ka(K,W).*(((xxxxs1(K,W))+sqrt((xxxxs1(K,W)).^2-1)).^(n-1)-xs(K,W)+ys(K,W)))));
Ricond = @(K, W) Ritesh(K,W) ./ 1; %xxxs(K,W) <1; %< 1;
[KK, WW] = ndgrid(K, W);
Zric = Ricond(KK, WW)
surf(KK,WW,real(Zric), 'edgecolor','none');
else
end
%%%%%%%%%%%%%%%%%%%%%%%%
in this programme we have to calculate function ritesh for condition xxxxs<1 for which xxxxs1=xxxxs which is the part of the function ritesh. here xxxxs is the row matrix array which has the element equal to the refine value. we have to accept those value of xxxxs which has less then 1 and then compute "ritesh" and then plot the solution with KK and WW.
Walter Roberson
Walter Roberson le 7 Juin 2016
It is not possible to do what you want to do.
surf() needs as input a 2D matrix of X values, a 2D matrix of Y values, and a 2D matrix of Z values. Every element of those must exist -- MATLAB does not permit "ragged" arrays or arrays with holes.
Your code inherently requires arrays with holes in it, because you refuse to define a value for the positions where xxxs(K,W) >= 1.
The code I built (and tested) for you defines some value for every grid position. It takes the short-cut of evaluating the main function at every location on the grid (not just where xxxs(K,W) < 1), but then taking care that every location where xxxs(K,W) < 1 is false gets given a value that is either -inf, +inf, or nan. surf() will silently not draw anything at positions where the Z is -inf, +inf, or nan (it is a design feature of surf, not a bug.)
If you want to get any further, you will need to adopt a similar strategy: for every K, W pair, you must have some output, even if the value is nan or inf. This does not inherently require that you compute at every location and then invalidate some of them (like I do), but if you choose to compute at only some locations then you need to assign the outputs of those locations into appropriate places in a larger grid to surf() after everything is complete. If you do want to compute at only some locations then you should read about logical indexing.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by