How do I properly subtract two column vectors of class 'Double'

2 次查看(过去 30 天)
Hi!
Im having trouble subtracting two column vectors of class = 'Double'
I have the equation:
v = A*x - l
A*x is very similar to L (both being 595x1), so i expect very small differences in the output variable v.
However, when running this in my function the output results are not as expected.
I tried isolating A*x and L and get them out of my function alone. I then did the subtraction manually in the command window afterwards and it computed the result i was expecting.
What am I missing?
  3 个评论
Max Alger-Meyer
Max Alger-Meyer 2022-3-10
@Mohammed Sahan As Tosten mentioned, this code is trivial, and correct. So if the output isn't as expected, that likely means that either you are mistaken and the values of A*x and L are not similar, or at some point between when that operation takes place and when you look at the output, 'v' is getting changed. Be sure to put a breakpoint on the line that the subtraction takes place, and check the values of A*x and L in your command window. Then, put another breakpoint on the next line and check the values of 'v'. If the values of 'v' are as expected, then 'v' is likely getting modified later on in your function or script. You'll have to post the function and inputs if you need anything more than that for us to help you.
Mohammed Sahan
Mohammed Sahan 2022-3-10
编辑:Mohammed Sahan 2022-3-10
Ill copy from v = A*x - l to the end.
All variables below are mostly constants or calculated based on constants. Only v, sgg and s0 are of interest.
v = A*x-1; % Residuals
% 9
g_first = g0 - gamma.*z0; % Reference height correction
% 10
dg_transfer = (h_factory + h_su- h_transfer)*gamma; % Transfer height correction
g_transfer = g_first + dg_transfer;
% 11
g = g_transfer + dg_set + dg_otl + dg_atl + dg_pt; % Corrections for SET, OTL, ATL, PT
% 12
sgg = sqrt((v'*v)/(n-e)) * sqrt(Q(3,3))
%13
s0 = sqrt((v'*v)/(n-e));
As seen above, i cannot find any place where i modify my 'v' after computing it.
As an example of what i expect ill use the two first rows from A*x and l:
Ax = [0.007912389029202;
0.008228885538678]
l = [0.007912389934100;
0.008228885531464]
The values are very similar but there should be some difference at around the 9th decimal place.
When computing v in the function i get the following results for the two first rows:
v = [-0.992087610970798;
-0.991771114461323]
This is clearly not correct, when im expecting something like:
% row1 = 0.007912389029202 - 0.007912389934100 = -9.04898e-10
% row2 = 0.008228885538678 - 0.008228885531464 = 7.2140002e-12
I end up getting the output i expect when calling out A*x and l from the function beforehand and then computing 'v', but not when i calculate it inside the function.

请先登录,再进行评论。

回答(1 个)

Torsten
Torsten 2022-3-10
You calculate
v = A*x - 1
in words: A*x - one
instead of
v = A*x - l
in words: A*x - el

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by