subplot of trigonometric function

Hi,
I want to make a 3x1 subplot (1 column and 3 row) for 3 trigonometric graphs
y = sin(x)
y= cos(x)
y = tan(x)
for x = 1:0.1:50
What code to do this? Thank you.

Answers (2)

A = @(x) sin(x);
subplot(3,1,1)
fplot(A,[1 50])
B = @(x) cos(x);
subplot(3,1,2)
fplot(B,[1 50])
C = @(x) tan(x);
subplot(3,1,3)
fplot(C,[1 50])

2 Comments

Thank you. What code I use to change title for each graph, axis label, and line color?
The Formatting and Annotation chapter in the documentation describes functions, tools, and techniques you can use to customize the appearance of your plots.

Sign in to comment.

dpb
dpb 44 minutes ago
Edited: dpb 2 minutes ago
Another random, seemingly unrelated post brought this thread up again; another couple of ways with more recent tools as comparison...
First with the not-so-newfangled-any-more @doc:tiledlayout
funcs={'sin','cos','tan'}; % the desired functions
rnge=[1 50]; % and plotting range
hTL=tiledlayout(3,1); % create the tile layout in desired format
for f=funcs % iterate over each functions in turn
nexttile % create the new axes to plot into
hF=str2func(f{:}); % and the function to plot as handle
fplot(hF,rnge) % plot it
title(f) % label the plot function
ylabel('Magnitude') % each y axis
ylim;
end
xlabel('X') % one generic for x since all same...
Alternatively, since is a common x-axis, this would be an application for a stacked plot albeit one has to create the data explicitly instead of being able to use the functional form. That might be an enhancement; have to think about that a little...
YLIM=arrayfun(@(h)ylim(h),flip(hTL.Children),'UniformOutput',false);
YTIK=arrayfun(@(h)yticks(h),(hTL.Children),'UniformOutput',false);
figure
hF=cellfun(@str2func,funcs,'UniformOutput',false);
x=linspace(rnge(1),rnge(2));
y=cell2mat(cellfun(@(h)h(x).',hF,'UniformOutput',false));
hSP=stackedplot(x,y,'DisplayLabels',funcs,'XLabel','X');
set(hSP.AxesProperties,{'YLimits'},YLIM) % make ylimits match
Unfortunately, the stacked plot default presentation isn't as pretty as what fplot() does on scaling so to make the two similar the above saved the ylim values from the first plot and applied them to the latter. I couldn't get the YTick values to match; it isn't an exposed property and handle-diving to the underlying axes showed the YLim values of [-1 1] for the top two but the tick labels wouldn't show up if set YTick to [-1 0 1] as in the tiled layout figure. All in all, not a pleasing result with stackedplot.

Tags

Asked:

on 24 Oct 2019

Edited:

dpb
about 2 hours ago

Community Treasure Hunt

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

Start Hunting!