Function contain the term I dont understand

To Define initial condition they define a function but I dont understand the following statement
ui = uR+(uL-uR)*((x-xshift)<= 0) , <=0 how matlab understand this step
I think it will be something similar the step function but I dont understand why we put <=0
function ui = uinit1(x)
xshift=0.5;
uL=1;
uR=0;
ui = uR+(uL-uR)*((x-xshift)<= 0);
end

Answers (1)

The ‘<=0’ creates a logical vector, so that if ‘(x-xshift)’ is less than zero, the value for that sub-expression is true or 1, and if it greater than 0, the value is false or 0. When used in a numeric calculation such as in this expression, the true values become numeric 1 and the false values become numeric 0.
ui = uR+(uL-uR)*((x-xshift)<= 0);
(I have no idea what the expression is actually calculating otherwise.)

4 Comments

Thank you so much can I ask what happen if x=xshift ?
As always, my pleasure.
If x=xshift, that will evaluate as true, because ‘<=’ means ‘less than or equal to’, and in that situation, (x-xshift) would be equal to 0, matching the ‘equal to’ condition. (The actual MATLAB code for that logical test would use the ‘double equal’ sign, or x == xshift.)
So as long as ‘x’ is less than or equal to ‘xshift’, the condition will return as true.
If ‘x’ is greater than ‘xshift’, the sub-expression is positive, and would evaluate as false.
Insıde a function they draw the graph of above function I add a graph as a file, normally in step function we dont draw straight line but here at x=0.5 we see vertical line because of that I asked actually what happen x=0.5
function %%%%%
clear
clc
J=300;
N=400;
Xmin=0;
Xmax=1;
X = linspace(Xmin, Xmax, J);
deltaX = X(2)-X(1);
u(1,:)=uinit1(X);
CFL =0.2;
deltaT=CFL*deltaX;
figure
hold on
%grid on
xlabel('r')
ylabel('u')
title([' n = ' num2str(N)]) %', \Deltat = ' num2str(deltaT),', \Deltar = ' num2str(deltaX)])
axis([Xmin Xmax -0.15 1.15])
x1 = X(1:J);
plot(x1,u(1,1:J),'k')
Thank you so much again
As always, my pleasure.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 27 Jan 2018

Edited:

on 27 Jan 2018

Community Treasure Hunt

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

Start Hunting!