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

2 vues (au cours des 30 derniers jours)
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 commentaires
Image Analyst
Image Analyst le 25 Oct 2015
It doesn't run because you did not include the "height()" function.
Ruten9
Ruten9 le 25 Oct 2015
Modifié(e) : Walter Roberson le 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.

Connectez-vous pour commenter.

Réponses (2)

Thorsten
Thorsten le 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 le 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 commentaires
Walter Roberson
Walter Roberson le 26 Oct 2015
The h that the poster created was a function handle. You cannot compare a function handle to 0.
Image Analyst
Image Analyst le 26 Oct 2015
Oh, right. That was unnecessary. Thorsten corrected it. Please Accept his answer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by