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

4 views (last 30 days)
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
Ruten9
Ruten9 on 25 Oct 2015
Edited: Walter Roberson on 26 Oct 2015
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)

Thorsten
Thorsten on 26 Oct 2015
function h = height(t)
h = ((-9.8).*(2.^(-1))).*(t (:) .^2) + 125.*t(:) + 500;
h(h<0) = 0;
end

Image Analyst
Image Analyst on 25 Oct 2015
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

Sign in to comment.

Categories

Find more on Graphics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!