Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
if, elseif,else, functions defined on intervals

Subject: if, elseif,else, functions defined on intervals

From: orken_m

Date: 7 Feb, 2011 14:53:03

Message: 1 of 3

Hi,

I think I am facing a simple problem here.
I want to construct a new function Pn, which is defined as a ratio of two functions
tauA and tauB on interval x>0.5, and is defined as a ratio of tauB and tauA on the interval x<=0.5. I wrote the Matlab code below, but it seems it does calculate pN function ALWAYS as a ratio of tauB to tauA, so it does not calculate it in a way I want on in the interval x>0.5.

I also tried other specifications with while, and elseif .. it does not work. Can you pls help? Many thanks.

x=linspace(0,1);
y = 0.5;
tauA=(y./(1-x)).^(1/2);
tauB=(x./(1-y)).^(1/2);
plot(x,tauA,x,tauB)
if x>0.5
    pN=tauA./tauB;
else
    pN=tauB./tauA;
end
plot(x,tauA,x,tauB,x,pN)

Subject: if, elseif,else, functions defined on intervals

From: Matt Fig

Date: 7 Feb, 2011 15:06:03

Message: 2 of 3

x=linspace(0,1);
y = 0.5;
tauA=(y./(1-x)).^(1/2);
tauB=(x./(1-y)).^(1/2);
idx = x>.5; % Logical indexing
pN(idx)=tauA(idx)./tauB(idx);
pN(~idx)=tauB(~idx)./tauA(~idx);
plot(x,tauA,x,tauB,x,pN)

Subject: if, elseif,else, functions defined on intervals

From: orken_m

Date: 7 Feb, 2011 15:24:04

Message: 3 of 3

"Matt Fig" wrote in message <iip1or$ita$1@fred.mathworks.com>...
> x=linspace(0,1);
> y = 0.5;
> tauA=(y./(1-x)).^(1/2);
> tauB=(x./(1-y)).^(1/2);
> idx = x>.5; % Logical indexing
> pN(idx)=tauA(idx)./tauB(idx);
> pN(~idx)=tauB(~idx)./tauA(~idx);
> plot(x,tauA,x,tauB,x,pN)

Fantastic! It works! Thanks a lot.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us