Why am I not getting a vector output?

2 ビュー (過去 30 日間)
Dawid Brits
Dawid Brits 2019 年 10 月 17 日
コメント済み: Dawid Brits 2019 年 10 月 17 日
Hi All,
Ive writen this function that computes the gradient vector for a function, but it only gives me a single value output. What am I doing wrong?
gradlandscape(3, 3);
function [retx, rety] = gradlandscape(x, y)
h = 0.1;
retx = (landscape(x+h, y)-landscape(x-h, y))/(2*h);
rety = (landscape(x, y+h)-landscape(x, y-h))/(2*h);
function M = landscape(x, y)
M = H1(x, y) + H2(x, y);
function h1 = H1(x, y)
R1 = 2;
s1 = [-2;2];
n1 = 2;
h1 = (1./(1+(R1./sqrt((x-s1(1)).^2+(y(:)-s1(2)).^2)).^n1));
end
function h2 = H2(x, y)
R2 = 1;
s2 = [3;-1];
n2 = 1;
h2 = (1./(1+(R2./sqrt((x-s2(1)).^2+(y(:)-s2(2)).^2)).^n2));
end
end
end

採用された回答

Adam Danz
Adam Danz 2019 年 10 月 17 日
編集済み: Adam Danz 2019 年 10 月 17 日
That's because you're only asking for a single output (by default). If you want both outputs, you must include two outputs in your call to the function.
[retx, rety] = gradlandscape(3, 3)
If the intention of the function was to produce a single output that is a 1 x 2 vector instead of two scalar outputs, then you'll need to change the function like this:
function retxy = gradlandscape(x, y)
h = 0.1;
retxy(1) = (landscape(x+h, y)-landscape(x-h, y))/(2*h);
retxy(2) = (landscape(x, y+h)-landscape(x, y-h))/(2*h);
.
.
.
  1 件のコメント
Dawid Brits
Dawid Brits 2019 年 10 月 17 日
Thanks!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by