matlab的绘图很奇怪

时间:2015-10-26 02:39:01

标签: matlab plot

当我执行此绘图功能时,试图将我的数据绘制为红色实线,它不会绘制任何内容。

plot(1:n, exp(x), '-r')

但是,如果我仍然使用完全相同的数据将规格从红色实线更改为绿色圆圈,

plot(1:n, exp(x), 'og')
它会策划!为什么?

如果需要,以下是所有代码。

clear all;
close all;

fprintf('\n\nJustin White Th-9\n\n')

x = input('Input the value of x to be approximated: ');
se = input('Input the target approximate perecent relative error, se: ');

[apre, macexp, n] = f_macexpF15(x, se);

macexp = macexp(1:end-1);

plotyy( 1:n, macexp, 1:n, apre);
hold on;
plot(1:n, exp(x), '-r')

它在这里调用的功能

function[apre, macexp, n] = f_macexpF15(x, se)

fprintf('\nJustin White Th-9\n')

apre = 100*ones(1,3);

ms = [36 22 10];

macexp(1) = 1;
j = 1;
n = 1;
%% comments
    while apre >= se 
        macexp(j+1) = macexp(j) + x^j/factorial(j);
        apre(j) = 100 * ((macexp(j+1)-macexp(j))/macexp(j+1));
        j = j + 1;
        n = n + 1;
    end
    n = n - 1;
end

提前致谢

1 个答案:

答案 0 :(得分:2)

简单,n是一个长度为x的向量,而1:n只是一个标量值,如果输入不正确的话。因此,首先检查两个向量(xplot)是否具有相同的大小。 为什么?如果n命令有两个条目,一个条目是向量而另一个条目是标量,则MATLAB会将其视为n不同的绘图命令(First: bool is_Prime(int); #include <iostream> using namespace std; int main() { int m, n; n = 2; cout << "Your input: "; cin >> m; cout << "The prime numbers less than or equal to " << m << " are:" << endl; while ( n <= m) { is_Prime(m); if (true) { cout << n << endl; } n++; } return 0; } bool is_Prime(int m) { for (int n = 1; n < m; n++) { bool prime = true; for (int x = 2; x*x <= m; x++) { if (m % x == 0) { prime = false; } } if (prime = true) { return true; } } } Second: bool is_Prime(int m); #include <iostream> using namespace std; int main() { int m; cout << "Your input: "; cin >> m; cout << "The prime numbers less than or equal to " << m << " are:" << endl; is_Prime(m); return 0; } bool is_Prime(int m) { for (int n = 1; n < m; n++) { bool prime = true; for (int x = 2; x*x <= n; x++) { if (n % x == 0) { prime = false; } } if (prime) cout << n << " " << endl; } return 0; } 矢量的长度)。

相关问题