Matlab: error / accuracy when evaluating a vector

I noticed that for me there was unpredictable behavior when evaluating vectors. It seems like it really is as if it were straightforward than indexing in a loop. Can anyone help me? I know it probably explains how it performs each operation, so I need some clues on how to look for it.

thanks for the consultation in advance thanks

Example:

x=[0.05:.01:3]; n=size(x,2); y1 = (8*x-1)/(x)-(exp(x)); for i=1:n y2(i)=(8*x(i)-1)/(x(i))-(exp(x(i))); end a1=[x;y1]; a2=[x;y2]; plot(x,y1, 'red') hold on plot(x,y2, 'blue') 

here's the plot: http://i.stack.imgur.com/qAHD6.jpg

results:

 a2: 0.05 -13.0513 0.06 -9.7285 0.07 -7.3582 0.08 -5.5833 0.09 -4.2053 0.10 -3.1052 0.11 -2.2072 0.12 -1.4608 0.13 -0.8311 0.14 -0.2931 0.15 0.1715 0.16 0.5765 a1: 0.05 6.4497 0.06 6.4391 0.07 6.4284 0.08 6.4177 0.09 6.4068 0.10 6.3958 0.11 6.3847 0.12 6.3734 0.13 6.3621 0.14 6.3507 0.15 6.3391 0.16 6.3274 
+4
source share
2 answers

What would you like:

  y1 = (8*x-1)./(x)-(exp(x)); % note the ./ 

instead:

  y1 = (8*x-1)/(x)-(exp(x)); 

As an additional note, you can type help / to see what your original first statement really did. It efficiently executed (x'\(8*x-1)')' (note the backslash).

+6
source

Error in your first vector calculation y1. The problem is what you divide by x. The division of Vector / Matrix is ​​different from the division of an element and returns the result of a vector / matrix.

The solution is to perform stepwise division of these vectors. A striking vector notation for this is use. / (dot-divide):

 y3 = (8*x-1)./(x)-(exp(x)); 

You should now find that y2 and y3 are identical.

+3
source

Source: https://habr.com/ru/post/1415013/


All Articles