如何求解一个将绘制cos(x)的MATLAB脚本的X值范围从– pi到pi,并以参数更改行宽度?

发布于 2025-02-08 12:19:03 字数 3101 浏览 1 评论 0原文

我正在尝试编写一个MATLAB函数,该功能将使用黑星(*)绘制从–pi到PI的X值的COS(X)。它将在一个图形窗口中进行三次操作,并具有不同的线宽度。

宽度为1、2和3。

(注意:即使绘制了各个点而不是实线,线宽度属性也会改变这些点的大小。)如果没有参数传递给函数,则行 ,一个参数传递给函数,它是这些值的乘数 (例如,如果通过3,则线宽度为3、6和9)。线宽度将在图上的标题中打印出来。

我已经开始进步,通过在一个图中显示三个图,但是当必须传递函数调用的参数时,我不知道该语法如何适合其他条件。我总是以1、2和3的线路宽度的默认值。对您所看到的最佳源代码的任何帮助或重大更改将不胜感激。

function plotCos(varargin)
    
    % Default line widths
    lw1 = 1;
    lw2 = 2;
    lw3 = 3;
    
    % If a random number is passed as an argument
    % Example: If 3 is passed, the line widths will be 3, 6, and 9 
    if nargin == 1
        % Multiply the default line widths with that number
        lw1 = lw1 * varargin{1};
        lw2 = lw2 * varargin{1};
        lw3 = lw3 * varargin{1};
    end
    
    % Create the figure window
    f = figure();
    
    % Set the title
    f.Name = 'Plot of cos(x) from -pi to pi';
    
    % Set the position of the figure window to be in the center of the screen
    f.Position = [100 100 600 400];
    
    % Set the axes
    a1 = subplot(1, 3, 1);
    a2 = subplot(1, 3, 2);
    a3 = subplot(1, 3, 3);
    
    % Set the title of the first plot
    a1.Title.String = sprintf('Line width: %d', lw1);
    
    % Set the title of the second plot
    a2.Title.String = sprintf('Line width: %d', lw2);
    
    % Set the title of the third plot
    a3.Title.String = sprintf('Line width: %d', lw3);
    
    % Set the x and y axes
    a1.XAxis.Label.String = 'x';
    a1.YAxis.Label.String = 'cos(x)';
    a2.XAxis.Label.String = 'x';
    a2.YAxis.Label.String = 'cos(x)';
    a3.XAxis.Label.String = 'x';
    a3.YAxis.Label.String = 'cos(x)';
    
    % Set the x and y limits
    a1.XLim = [-pi, pi];
    a1.YLim = [-1, 1];
    a2.XLim = [-pi, pi];
    a2.YLim = [-1, 1];
    a3.XLim = [-pi, pi];
    a3.YLim = [-1, 1];
    
    % Create an array of values from -pi to pi, in steps of 0.1
    vals = -pi:0.1:pi;
    
    % Plot the cos(x) values with line width lw1
    plot(a1, vals, cos(vals), 'k*', 'LineWidth', lw1);
    
    % Plot the cos(x) values with line width lw2
    plot(a2, vals, cos(vals), 'k*', 'LineWidth', lw2);
    
    % Plot the cos(x) values with line width lw3
    plot(a3, vals, cos(vals), 'k*', 'LineWidth', lw3);
    
    % Set the x and y ticks
    a1.XTick = -pi:pi/2:pi;
    a1.YTick = -1:1;
    a2.XTick = -pi:pi/2:pi;
    a2.YTick = -1:1;
    a3.XTick = -pi:pi/2:pi;
    a3.YTick = -1:1;
    
    % Set the x and y grid lines
    a1.XGrid = 'on';
    a1.YGrid = 'on';
    a2.XGrid = 'on';
    a2.YGrid = 'on';
    a3.XGrid = 'on';
    a3.YGrid = 'on';
    
    % Display the line widths in the titles
    a1.Title.String = sprintf('Line width: %d', lw1);
    a2.Title.String = sprintf('Line width: %d', lw2);
    a3.Title.String = sprintf('Line width: %d', lw3);

    % Set the figure to be visible
    f.Visible = 'on';
    
    % Display the plot
    drawnow
    
    % Save the plot as a png image in the current folder
    saveas(f, 'plotCos.png');
end

单击以查看我的情节的进度

I am trying to write a MATLAB function that will plot cos(x) for x values ranging from –pi to pi in steps of 0.1, using black stars (*). It will do this three times across in one Figure Window, with varying line widths.

(Note: Even if individual points are plotted rather than a solid line, the line width property will change the size of these points.) If no arguments are passed to the function, the line widths will be 1, 2, and 3.

Else, an argument is passed to the function, it is a multiplier for these values
(e.g., if 3 is passed, the line widths will be 3, 6, and 9). The line widths will be printed in the titles on the plots.

I have started progress by displaying three plots in one figure but I do not know how the syntax works for an else condition when an argument has to be passed for the function call. I always end up with default values for line widths of 1, 2, and 3. Any help or major change to the source code you see best will be appreciated.

function plotCos(varargin)
    
    % Default line widths
    lw1 = 1;
    lw2 = 2;
    lw3 = 3;
    
    % If a random number is passed as an argument
    % Example: If 3 is passed, the line widths will be 3, 6, and 9 
    if nargin == 1
        % Multiply the default line widths with that number
        lw1 = lw1 * varargin{1};
        lw2 = lw2 * varargin{1};
        lw3 = lw3 * varargin{1};
    end
    
    % Create the figure window
    f = figure();
    
    % Set the title
    f.Name = 'Plot of cos(x) from -pi to pi';
    
    % Set the position of the figure window to be in the center of the screen
    f.Position = [100 100 600 400];
    
    % Set the axes
    a1 = subplot(1, 3, 1);
    a2 = subplot(1, 3, 2);
    a3 = subplot(1, 3, 3);
    
    % Set the title of the first plot
    a1.Title.String = sprintf('Line width: %d', lw1);
    
    % Set the title of the second plot
    a2.Title.String = sprintf('Line width: %d', lw2);
    
    % Set the title of the third plot
    a3.Title.String = sprintf('Line width: %d', lw3);
    
    % Set the x and y axes
    a1.XAxis.Label.String = 'x';
    a1.YAxis.Label.String = 'cos(x)';
    a2.XAxis.Label.String = 'x';
    a2.YAxis.Label.String = 'cos(x)';
    a3.XAxis.Label.String = 'x';
    a3.YAxis.Label.String = 'cos(x)';
    
    % Set the x and y limits
    a1.XLim = [-pi, pi];
    a1.YLim = [-1, 1];
    a2.XLim = [-pi, pi];
    a2.YLim = [-1, 1];
    a3.XLim = [-pi, pi];
    a3.YLim = [-1, 1];
    
    % Create an array of values from -pi to pi, in steps of 0.1
    vals = -pi:0.1:pi;
    
    % Plot the cos(x) values with line width lw1
    plot(a1, vals, cos(vals), 'k*', 'LineWidth', lw1);
    
    % Plot the cos(x) values with line width lw2
    plot(a2, vals, cos(vals), 'k*', 'LineWidth', lw2);
    
    % Plot the cos(x) values with line width lw3
    plot(a3, vals, cos(vals), 'k*', 'LineWidth', lw3);
    
    % Set the x and y ticks
    a1.XTick = -pi:pi/2:pi;
    a1.YTick = -1:1;
    a2.XTick = -pi:pi/2:pi;
    a2.YTick = -1:1;
    a3.XTick = -pi:pi/2:pi;
    a3.YTick = -1:1;
    
    % Set the x and y grid lines
    a1.XGrid = 'on';
    a1.YGrid = 'on';
    a2.XGrid = 'on';
    a2.YGrid = 'on';
    a3.XGrid = 'on';
    a3.YGrid = 'on';
    
    % Display the line widths in the titles
    a1.Title.String = sprintf('Line width: %d', lw1);
    a2.Title.String = sprintf('Line width: %d', lw2);
    a3.Title.String = sprintf('Line width: %d', lw3);

    % Set the figure to be visible
    f.Visible = 'on';
    
    % Display the plot
    drawnow
    
    % Save the plot as a png image in the current folder
    saveas(f, 'plotCos.png');
end

Click to see the progress of my plot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文