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:
plotting a graph with conditions on matlab, for? while? if?

Subject: plotting a graph with conditions on matlab, for? while? if?

From: munkey

Date: 7 May, 2011 13:02:04

Message: 1 of 3

I am trying to plot a grph of height versus time with three separate conditions the conditions are;

when t<=36

h=nthroot(((9*F*t)/pi),3);


when 36<t<=100

h=((F*t)/(pi*r^2))+H1;


when t>100

h=HT


where HT,H1,F and r are all known and definsed variables. I have defined t as a range of variable between 0 and 150 and used the if and else loops to try and define which formula to use for which t but it just uses the formula that fits the condition for the heighest value of t.

this is my script

%Coursework rough

clear all
clc

F=6.2;

H1=8.66; %Height of conical section
ConeT=(H1^3*pi)/(9*F); %Time taken to fill volume of cylinder

r=5; %radius of cylinder
H2=8; %height of the cylinder
CylinderT=(8*pi*r^2)/F; %Time taken to fill volume of cylinder once cone is full

HT=H1+H2; %Total height of the hopper
HopT=ConeT+CylinderT; %Total time to fill the volume of the hopper



t=[0 0.25 0.5 0.75 1 2 3 5 10 15 35 37];
hold on
if t<=ConeT;
    h=nthroot(((9*F*t)/pi),3);
    
    plot(t,h);

elseif ConeT<t<=CylinderT;
     h=((F*t)/(pi*r^2))+H1;
     
     plot(t,h);
     
     
else t>HopT;
     h=HT;
     
     plot(t,h)
     
end

hold off

Subject: plotting a graph with conditions on matlab, for? while? if?

From: Nasser M. Abbasi

Date: 8 May, 2011 09:35:42

Message: 2 of 3

On 5/7/2011 6:02 AM, munkey wrote:
> I am trying to plot a grph of height versus time with three separate conditions the conditions are;
>
> when t<=36
>
> h=nthroot(((9*F*t)/pi),3);
>
>
> when 36<t<=100
>
> h=((F*t)/(pi*r^2))+H1;
>
>
> when t>100
>
> h=HT
>
>
> where HT,H1,F and r are all known and definsed variables. I have defined
> t as a range of variable between 0 and 150 and used the if and else loops
> to try and define which formula to use for which t but it just uses the
> formula that fits the condition for the heighest value of t.
>
> this is my script
>
> %Coursework rough
>
> clear all
> clc
>
> F=6.2;
>
> H1=8.66; %Height of conical section
> ConeT=(H1^3*pi)/(9*F); %Time taken to fill volume of cylinder
>
> r=5; %radius of cylinder
> H2=8; %height of the cylinder
> CylinderT=(8*pi*r^2)/F; %Time taken to fill volume of cylinder once cone is full
>
> HT=H1+H2; %Total height of the hopper
> HopT=ConeT+CylinderT; %Total time to fill the volume of the hopper
>
>
>
> t=[0 0.25 0.5 0.75 1 2 3 5 10 15 35 37];
> hold on
> if t<=ConeT;
> h=nthroot(((9*F*t)/pi),3);
>
> plot(t,h);
>
> elseif ConeT<t<=CylinderT;
> h=((F*t)/(pi*r^2))+H1;
>
> plot(t,h);
>
>
> else t>HopT;
> h=HT;
>
> plot(t,h)
>
> end
>
> hold off



you cant type 'if t<number', t is an array !

try this:

------------------------------------------
clear all
clc
figure(1); hold on;

F=6.2;
H1=8.66; %Height of conical section
ConeT=(H1^3*pi)/(9*F); %Time taken to fill volume of cylinder

r=5; %radius of cylinder
H2=8; %height of the cylinder
CylinderT=(8*pi*r^2)/F; %Time taken to fill volume of cylinder once cone is full

HT=H1+H2; %Total height of the hopper
HopT=ConeT+CylinderT; %Total time to fill the volume of the hopper

t1=0:.1:ConeT;
plot(t1,nthroot(((9*F*t1)/pi),3));

t2=ConeT:.1:CylinderT;
plot(t2,((F*t2)/(pi*r^2))+H1);

t3=CylinderT:.1:HopT;
plot(t3,ones(1,length(t3))*HT);
---------------------------------------

--Nasser

Subject: plotting a graph with conditions on matlab, for? while? if?

From: Steven_Lord

Date: 9 May, 2011 01:43:14

Message: 3 of 3



"munkey " <samcmunkeymail@hotmail.co.uk> wrote in message
news:iq3fsc$6s1$1@newscl01ah.mathworks.com...
> I am trying to plot a grph of height versus time with three separate
> conditions the conditions are;

*snip*

> t=[0 0.25 0.5 0.75 1 2 3 5 10 15 35 37];
> hold on
> if t<=ConeT;

http://www.mathworks.com/help/techdoc/ref/if.html

"An evaluated expression is true when the result is nonempty and contains
all nonzero elements (logical or real numeric). Otherwise, the expression is
false."

Therefore the body of the IF will only be executed if ALL the elements of t
are less than or equal to ConeT.

Use logical indexing instead.

http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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