simple x.y plot

3 views (last 30 days)
Sasha
Sasha on 5 Mar 2024
Commented: Torsten on 6 Mar 2024
%Q2) Create a variable 𝑥 which is a vector containing multiples of 5 between
% 0 and 50.
x = (0:5:50)
% Compute the function 𝑦 and plot it against 𝑥 using red colour dash line
% with circle markers:
y = (4*(x^2)*log(6))+(5*(x^2)) / sqrt (cos(x).^2) +1
plot (x,y, r--o)
  1 Comment
VBBV
VBBV on 5 Mar 2024
Markers inside the plot function are accessed using strtings/chars
%Q2) Create a variable 𝑥 which is a vector containing multiples of 5 between
% 0 and 50.
x = (0:5:50);
% Compute the function 𝑦 and plot it against 𝑥 using red colour dash line
% with circle markers:
y = ((4*(x.^2)*log(6))+(5*(x.^2))) ./ (sqrt (cos(x).^2)+1 );
plot (x,y, '--ro') % remember markers are accessed using strings/chars
Matlab intreprets any text placed outisde the chars/strings as variables of other data types and expects input values to those variables.
help plot
PLOT Linear plot. PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. If X is a scalar and Y is a vector, disconnected line objects are created and plotted as discrete points vertically at X. PLOT(Y) plots the columns of Y versus their index. If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)). In all other uses of PLOT, the imaginary part is ignored. Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none) no line y yellow s square k black d diamond w white v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram For example, PLOT(X,Y,'c+:') plots a cyan dotted line with a plus at each data point; PLOT(X,Y,'bd') plots blue diamond at each data point but does not draw any line. PLOT(TBL,XVAR,YVAR) plots the variables xvar and yvar from the table tbl. To plot one data set, specify one variable for xvar and one variable for yvar. To plot multiple data sets, specify multiple variables for xvar, yvar, or both. If both arguments specify multiple variables, they must specify the same number of variables PLOT(TBL,YVAR) plots the specified variable from the table against the row indices in the table. If the table is a timetable, the specified variable is plotted against the row times from the timetable. PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, where the X's and Y's are vectors or matrices and the S's are strings. For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a solid yellow line interpolating green circles at the data points. The PLOT command, if no color is specified, makes automatic use of the colors specified by the axes ColorOrder property. By default, PLOT cycles through the colors in the ColorOrder property. For monochrome systems, PLOT cycles over the axes LineStyleOrder property. Note that RGB colors in the ColorOrder property may differ from similarly-named colors in the (X,Y,S) triples. For example, the second axes ColorOrder property is medium green with RGB [0 .5 0], while PLOT(X,Y,'g') plots a green line with RGB [0 1 0]. If you do not specify a marker type, PLOT uses no marker. If you do not specify a line style, PLOT uses a solid line. PLOT(AX,...) plots into the axes with handle AX. PLOT returns a column vector of handles to lineseries objects, one handle per plotted line. The X,Y pairs, or X,Y,S triples, can be followed by parameter/value pairs to specify additional properties of the lines. For example, PLOT(X,Y,'LineWidth',2,'Color',[.6 0 0]) will create a plot with a dark red line width of 2 points. Example x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10) See also TITLE, XLABEL, YLABEL, XLIM, YLIM, LEGEND, HOLD, GCA, YYAXIS, PLOT3, SEMILOGX, SEMILOGY, LOGLOG, TILEDLAYOUT, HOLD, LEGEND, SCATTER Documentation for plot doc plot Other uses of plot alphaShape/plot antenna/plot Battery.Parameters/plot Battery.Pulse/plot Battery.PulseSequence/plot BayesianOptimization/plot blm/plot cfit/plot cgv.CGV/plot clustergram/plot clustering.evaluation.CalinskiHarabaszEvaluation/plot comm.MemorylessNonlinearity/plot conjugateblm/plot cornerPoints/plot customblm/plot cylinderModel/plot diffuseblm/plot digraph/plot driving.heremaps.LaneTopology/plot driving.Path/plot drivingScenario/plot dspdata/plot dtree/plot empiricalblm/plot fairnessMetrics/plot frd/plot graph/plot HeatMap/plot iddata/plot idnlarx/plot idnlhw/plot imageviewset/plot InflationCollisionChecker/plot KAZEPoints/plot lassoblm/plot LayerGraph/plot LeastSquaresResults/plot lime/plot LinearModel/plot matlab.buildtool.Plan/plot mixconjugateblm/plot mixsemiconjugateblm/plot monovslam/plot mpc/plot MSERRegions/plot nav/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 predmaint/plot prob.NormalDistribution/plot propagationData/plot quantumCircuit/plot RepeatedMeasuresModel/plot rfchain/plot roadrunnerHDMap/plot robotics/plot rocmetrics/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 tall/plot timeseries/plot tireData/plot tireModel/plot umargin/plot varbacktest/plot vehicleCostmap/plot wdectree/plot

Sign in to comment.

Answers (1)

Torsten
Torsten on 5 Mar 2024
Use
y = (4*(x.^2)*log(6))+(5*(x.^2)) ./ sqrt (cos(x).^2) +1
instead of
y = (4*(x^2)*log(6))+(5*(x^2)) / sqrt (cos(x).^2) +1
  2 Comments
Steven Lord
Steven Lord on 5 Mar 2024
Newer users tend to encounter this type of problem frequently as they start learning MATLAB. A couple releases ago (after 20 years I can't always remember offhand when we made certain changes :) I believe we enhanced the error message to try to provide more information about how to solve the problem.
%Q2) Create a variable 𝑥 which is a vector containing multiples of 5 between
% 0 and 50.
x = (0:5:50)
x = 1x11
0 5 10 15 20 25 30 35 40 45 50
% Compute the function 𝑦 and plot it against 𝑥 using red colour dash line
% with circle markers:
y = (4*(x^2)*log(6))+(5*(x^2)) / sqrt (cos(x).^2) +1
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
Is there any information you think could be added to this message or a way to rephrase some or all of it that would help users (especially newer users) to more effectively understand the problem and correct it themselves, without having to reach out to Answers or other MATLAB experts in their vicinity?
Torsten
Torsten on 6 Mar 2024
Is there any information you think could be added to this message or a way to rephrase some or all of it that would help users (especially newer users) to more effectively understand the problem and correct it themselves, without having to reach out to Answers or other MATLAB experts in their vicinity?
Maybe by including a link to a documentation page where Array vs. Matrix Operations are explained in examples ?
Like here:
?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!