Why is MatLab outputting 0s instead of the value?

1 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2022-9-17
E2 = (-1./(1+sec(x)))'
instead of
E2 = (-1/(1+sec(x)))'
  2 个评论
Jenah
Jenah 2022-9-17
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 2022-9-17
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).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by