Undifined function or variable x

5 ビュー (過去 30 日間)
Dawid Brits
Dawid Brits 2019 年 10 月 15 日
編集済み: Walter Roberson 2019 年 10 月 15 日
I am trying to create a matlab function that graphs a particular function in 3D, however I keep getting the error undifined variable or not enough input arguments, Here is my code.
R1 = 2;
R2 = 1;
s1 = [-2;2];
s2 = [3;-1];
n1 = 2;
n2 = 1;
%for values of x and y between -5 and 5
landscape(-5:5, -5:5);
function M = landscape(x, y)
M = H1 + H2;
function h1 = H1(x, y, R1, s1, n1)
h1 = (1/(1+(R1/sqrt((x-s1(1))^2+(y-s1(2))^2))^n1));
end
function h2 = H2(x, y, R2, s2, n2)
h2 = (1/(1+(R2/sqrt((x-s2(1))^2+(y-s2(2))^2))^n2));
end
surf(M)
contour(M)
end
%Thanks
  2 件のコメント
Stephen23
Stephen23 2019 年 10 月 15 日
編集済み: Stephen23 2019 年 10 月 15 日
You defined two nested functions H1 and H2, each with five input arguments:
function h1 = H1(x, y, R1, s1, n1)
then you call those functions withput any input arguments at all:
M = H1 + H2;
The variables R1, s1, n1 are not defined inside landscape.
Dawid Brits
Dawid Brits 2019 年 10 月 15 日
I fixed it up as follows
landscape(x, y, R1, s1, n1, R2, s2, n2)
function M = landscape(x, y, R1, s1, n1, R2, s2, n2)
M = H1(x, y, R1, s1, n1) + H2(x, y, R2, s2, n2);
function h1 = H1(x, y, R1, s1, n1)
h1 = (1/(1+(R1/sqrt((x-s1(1))^2+(y-s1(2))^2))^n1));
end
function h2 = H2(x, y, R2, s2, n2)
h2 = (1/(1+(R2/sqrt((x-s2(1))^2+(y-s2(2))^2))^n2));
end
surf(M)
contour(M)
end
But now it gives my an error about incorrect matrix operation, which I dont understand because Im only calling a single value from the matrix.

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 15 日
function better_homes_and_gardens
R1 = 2;
R2 = 1;
s1 = [-2;2];
s2 = [3;-1];
n1 = 2;
n2 = 1;
%for values of x and y between -5 and 5
landscape(-5:5, -5:5);
function M = landscape(x, y)
M = H1(x, y, R1, s1, n1) + H2(x, y, R2, s2, n2);
function h1 = H1(x, y, R1, s1, n1)
h1 = (1./(1+(R1/sqrt((x-s1(1)).^2+(y-s1(2)).^2)).^n1));
end
function h2 = H2(x, y, R2, s2, n2)
h2 = (1./(1+(R2/sqrt((x-s2(1)).^2+(y-s2(2)).^2)).^n2));
end
surf(M)
contour(M)
end
end
  4 件のコメント
Dawid Brits
Dawid Brits 2019 年 10 月 15 日
I added a couple more . s and now it gives me an error relating to M not being a matrix in the surf plot command? What am I doing wrong?
Walter Roberson
Walter Roberson 2019 年 10 月 15 日
編集済み: Walter Roberson 2019 年 10 月 15 日
function better_homes_and_gardens
R1 = 2;
R2 = 1;
s1 = [-2;2];
s2 = [3;-1];
n1 = 2;
n2 = 1;
%for values of x and y between -5 and 5
landscape(-5:5, -5:5);
function M = landscape(x, y)
M = H1(x, y, R1, s1, n1) + H2(x, y, R2, s2, n2);
function h1 = H1(x, y, R1, s1, n1)
h1 = (1./(1+(R1./sqrt((x-s1(1)).^2+(y(:)-s1(2)).^2)).^n1));
end
function h2 = H2(x, y, R2, s2, n2)
h2 = (1./(1+(R2./sqrt((x-s2(1)).^2+(y(:)-s2(2)).^2)).^n2));
end
surfc(M)
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePole and Zero Locations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by