Why isn't addition working for this equation
조회 수: 7 (최근 30일)
이전 댓글 표시
My script is not giving me the correct calculations
clear all
x = 0:50
y = 0:30
[X,Y] = meshgrid(x,y)
L1 = sqrt(((X-5).^2)+((Y-5).^2));
L2 = sqrt(((X-10).^2)+((Y-20).^2));
L3 = sqrt(((X-30).^2)+((Y-10).^2));
L = L1+L2+L3
If I do L1+L2 only, the sum is correct. However, when I do L1+L2+L3, the values I get are completely wrong and much less than L1+L2, which makes no sense at all.
Any help please.
댓글 수: 0
답변 (3개)
Jan
2017년 4월 28일
You do not explain how you come to the statement, that L1+L2+L3 is "completely wrong" and even smaller than L1+L2. I cannot observe this and I agree that this would not make sense at all:
[X,Y] = meshgrid(0:50, 0:30);
L1 = sqrt(((X-5).^2)+((Y-5).^2));
L2 = sqrt(((X-10).^2)+((Y-20).^2));
L3 = sqrt(((X-30).^2)+((Y-10).^2));
L12 = L1 + L2;
min(L12(:)) % 15.8113883008419
max(L12(:)) % 92.7092069611116
L123 = L1 + L2 + L3;
min(L123(:)) % 36.1585432929098
max(L123(:)) % 120.993478208574
any(L123(:) < L12(:)) % false
This looks fine. Nothing gets smaller. Everything looks completely correct. Therefore I assume, that your test is wrong, not the values.
댓글 수: 0
Steven Lord
2017년 4월 28일
x = 0:50
y = 0:30
[X,Y] = meshgrid(x,y)
L1 = sqrt(((X-5).^2)+((Y-5).^2));
L2 = sqrt(((X-10).^2)+((Y-20).^2));
L3 = sqrt(((X-30).^2)+((Y-10).^2));
L12 = L1+L2;
L123 = L1+L2+L3;
Run that code. Please tell us the index of a specific element in L12 and L123 where the value is "completely wrong" and show us the values in L1, L2, L3, L12, and L123 for that specific index, something like:
L123(2, 3) is not what I expect. I expected it to be 73.
>> [L1(2, 3); L2(2, 3); L3(2, 3); L12(2, 3); L123(2, 3)]
ans =
5.0000
20.6155
29.4109
25.6155
55.0264
If your X and Y matrices were square, I would have guessed you had used the ^ operator (matrix power) instead of the .^ operator (element-wise power) in computing L3. If you changed the x and y vectors when you copied the example from MATLAB to Answers, you may want to double-check that.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!