Function ranges in Matalb

조회 수: 3 (최근 30일)
Ahmed Elsherif
Ahmed Elsherif 2020년 8월 17일
댓글: Ahmed Elsherif 2020년 8월 21일
Hello everybody,
I am trying to use Gui matlab to determine the output of a function in two different ranges which are compared with user-entered values. I set the axis Z as
Z=0.000001:500;
The I used the entered values by user in calculations:
Z03= (4.*E.^1.6)/(r3.*0.8862269);% E and r3 are given by the user
Z04= (4.*E.^1.6)/(r4.*0.8862269);% E and r4 are given by the user
delat3=0;
delat4=0;
p3=-exp((-(Z-delat3)./Z03).^2);
p4=-exp((-(Z-delat4)./Z04).^2);
After that I defined the function's ranges as follows:
for i=1:numel(Z)
if Z(i)<=d3 % d3 is given by the user
P2L=(p3.*(2*(Z-delat3)./Z03))./((Z03).^2);
elseif (Z(i) > d3) && (Z(i) < d4) % d4 is given by the user
P2L=(p4.*(2*(Z-delat4)./Z04))./((Z04).^2);
end
end
and plot the results by
plot(Z, P2L,'linewidth',2)
When I press 'Plot' I got this error
Operands to the || and && operators must be convertible to logical scalar values.
Would you please help me to slove this error?
Thanks in advance,
Ahmed
  댓글 수: 6
dpb
dpb 2020년 8월 17일
편집: dpb 2020년 8월 17일
Well, whenever it was that the code posted before was run, the error message is clear that wasn't a scalar.
Use the debugger to see in context.
But, as written there's no need for the & operator anyways...and it is "&" you would want here, not "&&" if did need the compound test.
Ahmed Elsherif
Ahmed Elsherif 2020년 8월 21일
Thanks dpd, I already tried with Piecewise and it works nicely. Thanks for your help.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 8월 21일
for i=1:numel(Z)
if Z(i)<=d3 % d3 is given by the user
P2L(i)=(p3.*(2*(Z(i)-delat3)./Z03))./((Z03).^2);
elseif (Z(i) < d4) % d4 is given by the user
P2L(i)=(p4.*(2*(Z(i)-delat4)./Z04))./((Z04).^2);
else
P2L(i) = nan; %undefined outside the range given
end
end
  댓글 수: 1
Ahmed Elsherif
Ahmed Elsherif 2020년 8월 21일
Thanks a lot. I have it running but by using piecewise.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by