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

조회 수: 2 (최근 30일)
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
Image Analyst
Image Analyst 2015년 10월 25일
It doesn't run because you did not include the "height()" function.
Ruten9
Ruten9 2015년 10월 25일
편집: Walter Roberson 2015년 10월 26일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

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

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by