How can I make a flat,horizontal line appear in Matlab when my values are less than zero?

clear all; clc;
t = 0:.01:35;
h = height(t)
%Part C (maxima)---I swapped the order for the plotting
%max function
[xpsudeomax,ymax] = max(h(t));
xrealmax = t(ymax);
maxfunction = [xrealmax,ymax];
%fminbnd function
[xmax2,ymaxpsudeo]= fminbnd(@(t) (-1.*h(t)),0,30);
yrealmax = -1.*ymaxpsudeo;
format long g
fminbndmax = [xmax2,yrealmax] ;
%Part D --- awkward position was for plotting
[xzero,ypsuedozero]= fzero(@(t) h(t),20);
yrealzero = round(ypsuedozero);
ZERO = [xzero,yrealzero];
%Part B (height vs time)
figure;
plot(t,h(t),'blue',xmax2,yrealmax,'go','LineWidth',2);
hold on;
plot(xzero,yrealzero,'r.','MarkerSize',20);
ylabel('altitude [m]');
xlabel('time [sec]');
title('Rocket Trajectory');
legend('height','max','ground','location','ne')
axis([0 35 0 1500]);
If you load this, you'll get a graph that stops at the red dot. I want a blue line to appear on the x-axis after it touches the red dot. How can I do this?

2 Comments

It doesn't run because you did not include the "height()" function.
function h = height(t)
h(:,1) = @(t) ((-9.8).*(2.^(-1))).*(t (:) .^2) + 125.*t(:) + 500;
%h(h<0) = 0;
if h(t) <0
h=0
end
end
This is my height.m file . The commented section keeps giving me an error.

Sign in to comment.

Answers (2)

function h = height(t)
h = ((-9.8).*(2.^(-1))).*(t (:) .^2) + 125.*t(:) + 500;
h(h<0) = 0;
end
This is what I used:
function test3
clc; % Clear the command window.
t = 0:.01:35;
h = height(t)
%Part C (maxima)---I swapped the order for the plotting
%max function
[xpsudeomax,ymax] = max(h(t));
xrealmax = t(ymax);
maxfunction = [xrealmax,ymax];
%fminbnd function
[xmax2,ymaxpsudeo]= fminbnd(@(t) (-1.*h(t)),0,30);
yrealmax = -1.*ymaxpsudeo;
format long g
fminbndmax = [xmax2,yrealmax] ;
%Part D --- awkward position was for plotting
[xzero,ypsuedozero]= fzero(@(t) h(t),20);
yrealzero = round(ypsuedozero);
ZERO = [xzero,yrealzero];
%Part B (height vs time)
figure;
plot(t,h(t),'blue',xmax2,yrealmax,'go','LineWidth',2);
hold on;
plot(xzero,yrealzero,'r.','MarkerSize',20);
ylabel('altitude [m]');
xlabel('time [sec]');
title('Rocket Trajectory');
legend('height','max','ground','location','ne')
axis([0 35 0 1500]);
function h = height(t)
h(:,1) = @(t) ((-9.8).*(2.^(-1))).*(t (:) .^2) + 125.*t(:) + 500;
%h(h<0) = 0; if h(t) <0 h=0 end end
and this is what it produces:
Then you say "you'll get a graph that stops at the red dot. I want a blue line to appear on the x-axis after it touches the red dot"
I'm not sure exactly what that means. What I would suggest you try is to either plot a red circle instead of a dot using 'ro' or plot the blue line after you plot the red dot/circle.

6 Comments

There's a blue horizontal line after the red dot.
Like Thorsten says, uncomment the code that you commented out
h(h<0) = 0;
Why did you comment it out in the first place?
The h that the poster created was a function handle. You cannot compare a function handle to 0.
Oh, right. That was unnecessary. Thorsten corrected it. Please Accept his answer.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 25 Oct 2015

Commented:

on 26 Oct 2015

Community Treasure Hunt

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

Start Hunting!