首页 > 解决方案 > 在改变一个参数的同时绘制函数

问题描述

我有一个 Matlab 函数,我想在将某个参数从 100 更改为 1000 的同时绘制它,如下所示: [c, errorrange, i] = function [g1, g2, x]; 我想在改变 x 的同时绘制 x 和 c

函数本身......

  function [root,e, i]=func(xl,xu,e_stopping, z)
    if ((((xl^4)/(20*z)-(3*z^2*xl^2)/50)*((xu^4)/(20*z)-(3*z^2*xu^2)/50)) > 0) 
        error('the intials wont work');
    end
    root = z/2+z*2;
    e = 2;
    i = 2;
    end

…………………………………………………………………………

我想到了这样的事情:

...

for i = 100: 1000
[c, error] = fund(1000, 2000,0, i);
A(i) = c;
end
for i = 1: 99
A(i) = 0;
end
plot(A); 

......但它不会工作; 抱歉,如果我的问题不好,我是 Matlab 新手。谢谢

标签: matlabmatlab-figurematlab-guide

解决方案


您的问题不清楚,因为您说要绘制的变量未在代码中标识。尽管您在函数中调用它们并不重要(就像您在评论中所说的那样),但如果您的问题不清楚,那么我们必须猜测 i=x 或 A=x 或任何您想要的.

我仍然不确定你想要绘制什么,但这应该足够接近让你开始。基本上,看起来您不需要任何for循环,您只需将向量输入传递给您的函数。

A = 1:1:100; % create this vector however you need
[c, error] = func(1000, 2000, 0, A);

plot(A, c)
xlabel('my input A', 'FontSize', 10)
ylabel('my function output c', 'FontSize', 10)
title("my graph", 'FontSize', 12)



function [root, e, i] = func(xl, xu, e_stopping, z)
    root = z/2 + z*2;
    e = 2;
    i = 2;
end

在此处输入图像描述


推荐阅读