I'd like to construct a function in SciLab that calculates the norm of each element of the vector?

42 Views Asked by At

I'm new to Scilab and trying to construct a function that calculates the norm of each element, which is a complex number.

For example, the vector is:

--> psi17
 psi17  = 

  -4.349D-17 - 0.0334936i
  -1.574D-17 - 0.0334936i
  -2.590D-17 - 9.206D-17i
  -2.590D-17 + 2.763D-17i
   0.4665064 - 7.819D-17i
   0.4665064 - 1.574D-17i
   5.365D-17 + 1.574D-17i
  -1.859D-18 + 7.992D-17i
  -0.5       + 0.125i    
   0.5       + 0.125i    
   7.633D-17 - 6.939D-18i
  -2.082D-17 + 7.980D-17i
   0.125     - 6.939D-17i
   0.125     + 1.388D-17i
  -2.082D-17 - 6.939D-18i
   3.469D-17 + 2.429D-17i

and I'd like to create function that returns the following column vector:

N =
 
  norm(psi17(1))
  norm(psi17(2))
  norm(psi17(3))
  ...
  norm(psi17(16))

I tried to implement the following function:

function N = calc_norm(state)
    for i=1:length(state)
        N(i) = norm(state(i))
    end
endfunction

I got the error:

--> exec('C:\Users\looka\Downloads\SimulacaoQuantum.sce', -1)
at line     3 of function calc_norm ( C:\Users\looka\Downloads\SimulacaoQuantum.sce line 21 )
at line   134 of executed file C:\Users\looka\Downloads\SimulacaoQuantum.sce

Invalid index.

I also tried this code:

function N = calc_norm(state)
    for i=1:length(state)
        N = [norm(state(i));N]
    end
endfunction

This one returned the error:

--> exec('C:\Users\looka\Downloads\SimulacaoQuantum.sce', -1)
Warning : redefining function: calc_norm               . Use funcprot(0) to avoid this message
at line     3 of function calc_norm ( C:\Users\looka\Downloads\SimulacaoQuantum.sce line 21 )
at line   134 of executed file C:\Users\looka\Downloads\SimulacaoQuantum.sce

Invalid index.
0

There are 0 best solutions below