How can I make a flat,horizontal line appear in Matlab when my values are less than zero?
4 次查看(过去 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 个评论
回答(2 个)
Thorsten
2015-10-26
function h = height(t)
h = ((-9.8).*(2.^(-1))).*(t (:) .^2) + 125.*t(:) + 500;
h(h<0) = 0;
end
0 个评论
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
2015-10-26
The h that the poster created was a function handle. You cannot compare a function handle to 0.
Image Analyst
2015-10-26
Oh, right. That was unnecessary. Thorsten corrected it. Please Accept his answer.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!