Matlab误差矩阵的维数必须一致

  • 本文关键字:误差 Matlab matlab matrix
  • 更新时间 :
  • 英文 :


我试图在matlab中编写一个代码,可以解决二次方程,但我在第41行代码的第一个比特中得到一个错误。代码的最后一位只是计算x的值,并告诉用户这些值是复数根还是正数

function quadraticsolver1
    clc
    clear all
    fprintf ('Welcome to Quadratic Solvern')
    fprintf ('Created by Rémi Tuyaerts 2013n')
    % Get value for a
    a=input('enter value for a ', 's');
    % if value of a is empty or not numeric ask user to reenter it
    while isempty(a); ischar(a);
        disp ('The value entered for a is incorrect')
        a=input('Please reenter value for a ', 's');
    end
    % Get value for b
    b=input('enter value for b ', 's');
    % if value of b is empty or not numeric ask user to reenter it
    while isempty(b); ischar (b);
        disp ('The value entered for b is incorrect')
        b=input('Please reenter value for b', 's');
    end
    % Get value for c
    c=input('enter value for c ', 's');
    % if value of c is empty or not numeric ask user to reenter it
    while isempty (c); ischar (c);
        disp ('The value entered for a is incorrect')
        c=input('Please reenter value for c ', 's');
    end
    % calculating the value of the sqrt
    g = (b.^2)-(4*a.*c);
end

我不确定是否有可能告诉没有输入的例子,你使用,但我认为问题是这样的:

c=input('Please reenter value for c ', 's');

's'表示输入被视为字符串,而不是数字。然后你试着把它作为函数末尾的一个数字,这显然是错误的。有时这实际上会给出一个答案,但我怀疑你输入的数字有不同的位数。这意味着,作为字符串,它们的大小是不同的,你会得到一个错误。

解决方案是从所有input函数中删除's'参数,因为您不需要它们。

EDIT:如果出于某种原因,您希望它们最初是字符串,您可以将字符串转换回数字,如下所示:

a = str2num(a);

最新更新