Function ranges in Matalb

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 comentarios

junzhuo ren
junzhuo ren el 17 de Ag. de 2020
After the first condition in the for loop,matlab automatically regard z(i)>d3,so it makes no sense to show it again.
Ahmed Elsherif
Ahmed Elsherif el 17 de Ag. de 2020
Editada: Ahmed Elsherif el 17 de Ag. de 2020
@Junzhuo ren: Sorry, where is 'again'? The first codition considers Z < d3 while the second codition in 'elseif' is evaluating the expression for Z >d3 but <d4.
dpb
dpb el 17 de Ag. de 2020
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
in
elseif (Z(i) > d3) && (Z(i) < d4) % d4 is given by the user
can't get there unless Z(i)>d3 because all cases of Z(i)<=d3 are in the other branch already...so, j r is pointing out the code is same as
for i=1:numel(Z)
if Z(i)<=d3 % d3 is given by the user
P2L=(p3.*(2*(Z-delat3)./Z03))./((Z03).^2);
else
if Z(i)<d4
P2L=(p4.*(2*(Z-delat4)./Z04))./((Z04).^2);
end
end
end
to separate out the two ... or, of course, you can leave the elseif in and just remove the superfluous test.
The error as you wrote is owing to the use of '&&' and that d3 which is not defined in the supplied code snippet must have been a vector, not a single element...observe at the command line if write:
>> v0 % a variable happened to be in my current workspace for convenience...
v0 =
10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00
>> v0(1) && 3 % short-circuit && and a constant
ans =
logical
1
>> v0(1) && [3 4] % short-circuit && and a vector
Operands to the || and && operators must be convertible to logical scalar values.
>>
NB: the latter goes "boom"...
Ahmed Elsherif
Ahmed Elsherif el 17 de Ag. de 2020
Dear dpb, thanks a lot for your comment.
Regarding d3 it is defined under the edit box:
function edit6_Callback(hObject, eventdata, handles)
% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
% d3=str2double(get(handles.edit6, 'string'))
d3=get(handles.edit6, 'string')
setappdata(0,'edit6',d3)
Then I got it under pushbutton as:
d3=getappdata(0,'edit6')
d3 = str2num(get(handles.edit6,'String'));
I see it is a single value!
When I checked as you porposed, I got
>> d3
d3 =
20
>> d3(1)&&3
ans =
logical
1
>> d3(1)&&[3 4]
Operands to the || and && operators must be convertible to logical scalar values.
dpb
dpb el 17 de Ag. de 2020
Editada: dpb el 17 de Ag. de 2020
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 el 21 de Ag. de 2020
Thanks dpd, I already tried with Piecewise and it works nicely. Thanks for your help.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 21 de Ag. de 2020

0 votos

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 comentario

Ahmed Elsherif
Ahmed Elsherif el 21 de Ag. de 2020
Thanks a lot. I have it running but by using piecewise.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2019b

Preguntada:

el 17 de Ag. de 2020

Comentada:

el 21 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by