Errors computing signals on Matlab

Viewed 39

I'm trying to write in Matlab a script to create 3 signals, that have to be presented in the same graph (using plot). My code, in this moment, is

    n = 0:80;

    s = 2*n*(0.9).^n;      %signal 1 (can be a real or an imaginary signal)
    r = rand(1, 81) - 0.5; %signal 2 (can be a real or an imaginary signal)

    x = s + r;             %signal 3: it is supposed to be the sum of the other 2 signals
    xr = real(x);
    xi = imag(x);
    x = xe + xi;

   ER = XXX_A_B(x, s);

   plot(n, s, n, r, n, x, n, ER); %plot
   title('s[n], r[n], x[n], ER) %ER: Error related to the approximation of x to s
   legend('s[n]', 'r[n]','x[n]', 'ER')

I had already done a function to compute ER, so I used it to calculate it now. The function is

   function [ER] = XXX_A_B(x1, x2)
      x = abs(x2-x1);
      L2 = norm(x, 2);
      L2_1 = norm(x1, 2);
      ER = L2/L2_1;
   end

It happens that already in the first line, it appears an error (not saying of what kind) and another on x = s + r. I'm not understanding the reason of these mistakes. Thanks.

1 Answers

One issue is that the matrix dimensions cause errors. Depending on your intended function matrix multiplication by taking the transpose or element-wise multiplication might fix the first issue. Another issue is xe is not defined before using it as the comment above outlines.

n = 0:80; %1 by 81 array%

    Part_Of_Equation = (0.9).^n; %1 by 81 array%   
    
    %Taking transpose of n -> n.' (matrix multiplication)%
    s = 2*n.'*Part_Of_Equation;   
    
    %Or multipling element-wise%
    s = 2*n.*Part_Of_Equation; 

Using MATLAB version: R2019b

Related