why are my graphs not publishing?

15 views (last 30 days)
Marina Osorio
Marina Osorio on 12 Oct 2021
Edited: Mitsu on 12 Oct 2021
%% Problem 3
%Part a: Create a function called height that accepts time as an imput and
%returns the height of the rocket.
function h = height(t)
% HEIGHT Brief summary of this function.
%
% Detailed explanation of this function.
h = (-(5.4/2).*t.^2)+(130.*t)+450;
%%
%Part b: Plot height versus time for times from 0 to 30 seconds and
%incriment of 0.5 seconds
figure(1);
t=[0:0.5:30];
h=height(t);
plot(t,h);
title('Path of the rocket');
xlabel('Time');
ylabel('Height');
%%
%Part c: Find the time wen the rocket starts to fall back to the ground
t=[0:0.5:30];
h=height(t);
[h_max,time]=max(h);
T_hmax=t(time);
fprintf('The time at which the rocket starts fallins in %d sec \n',T_hmax);
The time at which the rocket starts fallins in 24 sec
end
%% Problem 4
%Part a: Create a function hanfle to the highet function
function height = height_handle(t)
% HEIGHT_HANDLE Brief summary of this function.
%
% Detailed explanation of this function.
height = (-(5.4/2).*t.^2)+(130.*t)+450;
%%
%Part b: Use height_handle as input to the fplot function, and crate a
%graph from 0-60 seconds
height=@(t)-(5.4/2).*t.^2+(130.*t)+450;
height =
function_handle with value:
@(t)-(5.4/2).*t.^2+(130.*t)+450;
figure(2)
fplot(height,[0,60])
title('Height vs. Time')
xlabel('Time')
ylabel('Height')
%%
%Use the fzero function to find the time when the rockt hits the ground
fzero(height,0);
ans =
-3.2431
end
%% Problem 5
%Create a function that crates a polygon with n mumber of sides.
function polygon()
% POLYGON Brief summary of this function.
%
% Detailed explanation of this function.
n=input('Enter the number of sides for the polygon:\n');
theta=[0:2.*pi/n:2*pi];
r=ones(1,length(theta));
polar(theta,r);
title('Polygon');
%Run the function
figure(3);
polygon
Enter the number of sides for the polygon:
3
figure(4);
polygon
Enter the number of sides for the polygon:
4
figure(5);
polygon
Enter the number of sides for the polygon:
5
figure(6);
polygon
Enter the number of sides for the polygon:
6
end

Answers (1)

Mitsu
Mitsu on 12 Oct 2021
Edited: Mitsu on 12 Oct 2021
Firstly,
For problem 3 and problem 4, you can solve your issue by adding hold on after you define the figure.
So instead of
figure(1);
t=[0:0.5:30];
h=height(t);
plot(t,h);
title('Path of the rocket');
xlabel('Time');
ylabel('Height');
you could use
figure(1);
hold on;
t=[0:0.5:30];
h=height(t);
plot(t,h);
title('Path of the rocket');
xlabel('Time');
ylabel('Height');
or even better:
ha = axes('NextPlot','add');
t=[0:0.5:30];
h=height(t);
plot(ha,t,h);
title(ha,'Path of the rocket');
xlabel(ha,'Time');
ylabel(ha,'Height');
Secondly,
I am not sure how exactly you're calling these functions, but in Problem 3 there is a recursive call:
function h = height(t)
...
h=height(t);
...
end
that might break your computations. Maybe that is the problem you're actually asking about.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by