How do I plot a graph with the grid for trig functions with 0 ≤ x ≤ 2π

28 vues (au cours des 30 derniers jours)
Sean
Sean le 3 Nov 2025 à 15:12
Modifié(e) : Sean le 3 Nov 2025 à 19:46
y1 = cos (x) ... solid line, red, no marker
y2 = -sin (x) ... dashed line, blue, no marker
y3 = cos (x)’ ... no line, blue, circle
Cos(x)' is the ferivative of cos(x)

Réponse acceptée

John D'Errico
John D'Errico le 3 Nov 2025 à 16:31
syms x
f = cos(x)
f = 
dfdx = diff(f)
dfdx = 
help fplot
fplot - Plot expression or function This MATLAB function plots the curve defined by the function y = f(x) over the default interval [-5 5] for x. Syntax fplot(f) fplot(f,xinterval) fplot(funx,funy) fplot(funx,funy,tinterval) fplot(___,LineSpec) fplot(___,Name,Value) fplot(ax,___) fp = fplot(___) [x,y] = fplot(___) Input Arguments f - Function to plot function handle xinterval - Interval for x [–5 5] (default) | two-element vector of form [xmin xmax] funx - Parametric function for x coordinates function handle funy - Parametric function for y coordinates anonymous function | function handle tinterval - Interval for t [-5 5] (default) | two-element vector of form [tmin tmax] ax - Axes object axes object LineSpec - Line style, marker, and color string scalar | character vector Name-Value Arguments MeshDensity - Number of evaluation points 23 (default) | number Color - Line color [0 0.4470 0.7410] (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... LineStyle - Line style "-" (default) | "--" | ":" | "-." | "none" LineWidth - Line width 0.5 (default) | positive value Marker - Marker symbol "none" (default) | "o" | "+" | "*" | "." | ... MarkerEdgeColor - Marker outline color "auto" (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerFaceColor - Marker fill color "none" (default) | "auto" | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerSize - Marker size 6 (default) | positive value Output Arguments fp - One or more FunctionLine or ParameterizedFunctionLine objects scalar | vector Examples openExample('graphics/PlotExpressionExample') openExample('graphics/PlotParametricCurveExample') openExample('graphics/SpecifyPlottingIntervalAndPlotPiecewiseFunctionsExample') openExample('graphics/SpecifyLinePropertiesAndDisplayMarkersExample') openExample('graphics/ModifyPlotAfterCreationExample') openExample('graphics/PlotMultipleLinesInSameAxesExample') openExample('graphics/AddTitleAndAxisLabelsAndFormatTicksExample') See also fcontour, fmesh, fplot3, fsurf, hold, title, fimplicit, FunctionLine Properties, ParameterizedFunctionLine Properties Introduced in MATLAB before R2006a Documentation for fplot doc fplot Other uses of fplot symbolic/fplot

Plus de réponses (1)

Sam Chak
Sam Chak le 3 Nov 2025 à 15:36
You have likely not yet looked up the plot() function. There are several examples.
help plot
plot - 2-D line plot This MATLAB function creates a 2-D line plot of the data in Y versus the corresponding values in X. Vector and Matrix Data plot(X,Y) plot(X,Y,LineSpec) plot(X1,Y1,...,Xn,Yn) plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) plot(Y) plot(Y,LineSpec) Table Data plot(tbl,xvar,yvar) plot(tbl,yvar) Additional Options plot(ax,___) plot(___,Name,Value) p = plot(___) Input Arguments X - x-coordinates scalar | vector | matrix Y - y-coordinates scalar | vector | matrix LineSpec - Line style, marker, and color string scalar | character vector tbl - Source table table | timetable xvar - Table variables containing x-coordinates string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype() yvar - Table variables containing y-coordinates string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype() ax - Target axes Axes object | PolarAxes object | GeographicAxes object Name-Value Arguments Color - Line color RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... LineStyle - Line style "-" (default) | "--" | ":" | "-." | "none" LineWidth - Line width 0.5 (default) | positive value Marker - Marker symbol "none" (default) | "o" | "+" | "*" | "." | ... MarkerIndices - Indices of data points at which to display markers 1:length(YData) (default) | vector of positive integers | scalar positive integer MarkerEdgeColor - Marker outline color "auto" (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerFaceColor - Marker fill color "none" (default) | "auto" | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerSize - Marker size 6 (default) | positive value DatetimeTickFormat - Format for datetime tick labels character vector | string DurationTickFormat - Format for duration tick labels character vector | string Examples openExample('graphics/CreateLinePlotExample') openExample('graphics/PlotMultipleLinesExample') openExample('graphics/CreateLinePlotFromMatrixExample') openExample('graphics/SpecifyLineStyleExample') openExample('graphics/SpecifyLineStyleColorAndMarkerExample') openExample('graphics/DisplayMarkersAtSpecificDataPointsExample') openExample('graphics/SpecifyLineWidthMarkerSizeAndMarkerColorExample') openExample('graphics/AddTitleAndAxisLabelsExample') openExample('matlab/SpecifyDurationTickFormatsExample') openExample('graphics/PlotTableDataExample') openExample('graphics/PlotTableMutliVariablesExample') openExample('graphics/PlotSpecifyAxes19bExample') openExample('graphics2/ChangeLinePropertiesUsingHandlesExample') openExample('graphics/PlotCircleExample') See also title, xlabel, ylabel, xlim, ylim, legend, hold, gca, yyaxis, plot3, loglog, Line Properties Introduced in MATLAB before R2006a Documentation for plot doc plot Other uses of plot AggregateBayesianOptimization/plot alphaShape/plot antenna.Circle/plot backscatterBicyclist/plot backscatterPedestrian/plot Battery.Parameters/plot Battery.Pulse/plot Battery.PulseSequence/plot BayesianOptimization/plot blm/plot cfit/plot cgdatasetnode/plot cgrules/plot cgv.CGV/plot climateScenario/plot clusterDBSCAN/plot clustergram/plot clustering.evaluation.CalinskiHarabaszEvaluation/plot comm.MemorylessNonlinearity/plot comm.Ray/plot conjugateblm/plot cornerPoints/plot customblm/plot cylinderModel/plot DataMatrix/plot diffuseblm/plot digraph/plot driving.heremaps.LaneTopology/plot driving.Path/plot drivingScenario/plot dtree/plot empiricalblm/plot esbacktest/plot eyeMask/plot fairnessMetrics/plot frd/plot graph/plot HeatMap/plot iddata/plot idnlarx/plot idnlhw/plot imageviewset/plot InflationCollisionChecker/plot KAZEPoints/plot laneBoundarySegment/plot lassoblm/plot LayerGraph/plot LeastSquaresResults/plot lidarScan/plot lime/plot LinearModel/plot localavfit/plot localmod/plot localmulti/plot matlab.buildtool.Plan/plot mixconjugateblm/plot mixsemiconjugateblm/plot monovslam/plot mpc/plot MSERRegions/plot NLMEResults/plot ntree/plot opc.hda.Data/plot opticalFlow/plot parkingSpace/plot pathPlannerRRT/plot pcviewset/plot phased.FMCWWaveform/plot phased.LinearFMWaveform/plot phased.MFSKWaveform/plot phased.PhaseCodedWaveform/plot phased.RectangularWaveform/plot phased.SteppedFMWaveform/plot phytree/plot polyshape/plot prob.NormalDistribution/plot propagationData/plot pulseWaveformLibrary/plot reidentificationmetrics/plot RepeatedMeasuresModel/plot rfchain/plot rgbdvslam/plot roadrunnerHDMap/plot rocmetrics/plot scenariobuilder.ActorTrackData/plot scenariobuilder.GPSData/plot scenariobuilder.Trajectory/plot semiconjugateblm/plot sfit/plot shapley/plot SimBiology.fit.ParameterConfidenceInterval/plot SimBiology.fit.PredictionConfidenceInterval/plot SimBiology.gsa.ElementaryEffects/plot SimBiology.gsa.MPGSA/plot SimBiology.gsa.Sobol/plot simscape.logging.Node/plot simscape.logging.Series/plot simscape.logging/plot Simulink.sdi.DatasetRef/plot Simulink.sdi/plot Simulink.SimulationData.Parameter/plot slrealtime.ProfilerData/plot stereovslam/plot sweepset/plot tall/plot tcnDetector/plot timeseries/plot tireData/plot tireModel/plot trajectoryErrorMetrics/plot umargin/plot varbacktest/plot vehicleCostmap/plot wdectree/plot xregmodel/plot xregtransient/plot xregtwostage/plot
  3 commentaires
Sam Chak
Sam Chak le 3 Nov 2025 à 16:48
Thanks for your clarification. If this is a routine exercise to obtain the derivative of a trig function, you can use the symbolic approach as demonstrated in @John D'Errico's answer. Otherwise, you can use the numerical approach:
x = -pi:0.001:pi;
y = cos(x);
dy = gradient(y)./gradient(x);
plot(x, [y; dy])
Sean
Sean le 3 Nov 2025 à 19:46
Modifié(e) : Sean le 3 Nov 2025 à 19:46
thank you, for the response both the symbolic aproach and numerical aproach are helpful!

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by