Why is MatLab outputting 0s instead of the value?

2 views (last 30 days)
I am trying to create a matrix of an equation, E2, evaluated at different values of x (in a matrix). Why is it outputting 0s in the E2 matrix, but when I evaluate the equation at a certain element of the x matrix individually, it can print it just fine?
>> format long
>> x
x =
0.100000000000000
0.010000000000000
0.001000000000000
0.000100000000000
0.000010000000000
0.000001000000000
0.000000100000000
0.000000010000000
0.000000001000000
0.000000000100000
0.000000000010000
0.000000000001000
0.000000000000100
0.000000000000010
>> E2 = (-1/(1+sec(x)))'
% here it seems that E2 is only printing first element of x array
E2 =
-0.498747913711429
0
0
0
0
0
0
0
0
0
0
0
0
0
% however, when I evaluate at different elements of the matrix seperately,
% it can print it... see...
>> E2 = (-1/(1+sec(x(2))))'
E2 =
-0.499987499791664
>> E2 = (-1/(1+sec(x(7))))'
E2 =
-0.499999999999999

Accepted Answer

Torsten
Torsten on 17 Sep 2022
E2 = (-1./(1+sec(x)))'
instead of
E2 = (-1/(1+sec(x)))'
  2 Comments
Jenah
Jenah on 17 Sep 2022
Thank you! That worked. Okay, but I also have E1 which isn't printing out all the values of the matrix. Do I need to adjust anything here?
E1 = ((1-sec(x))./(tan(x).^2))'
Torsten
Torsten on 17 Sep 2022
Yes.
If x is a vector and you want to *, / or ^ expressions f1(x) and f2(x) that depend on x, you must use elementwise multiplication, division and potentiation:
f1(x).*f2(x), f1(x)./f2(x), f1(x).^f2(x).

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!