필터 지우기
필터 지우기

variable needs to be array but only returning 1 value

조회 수: 10 (최근 30일)
Benjamin
Benjamin 2019년 3월 28일
댓글: Stephen23 2019년 3월 28일
I have a code that I am trying to vectorize but I must be missing something:
This code below is part of a bigger loop, but I think that is irrelevant for this:
r = 1:0.01:2;
g_fit = g(r,mat(bnd(k),3));
plot(r,g_fit,'k');
xlabel('r');
ylabel('g(r,\eta)');
hold on;
For some reason, g_fit only contains a single value. I tried putting parenthesis around the r in the call the function g, but that didn't help either. How can I get g to be a matrix that stores the result as r goes from 1 to 2 in increments of 0.01?
Here are the two functions:
function rdf = g(x,rpf)
rdf = x/((1+C(rpf)*(x-1)).^3);
end
function C_function = C(rpf)
B1 = 1.90322581;
B2 = 2.13209879;
B3 = -2.45289889;
C_function = (1.0+rpf*(B1+rpf*(B2+rpf*B3)))/(3.0*(1-rpf));
end

답변 (1개)

Walter Roberson
Walter Roberson 2019년 3월 28일
편집: Walter Roberson 2019년 3월 28일
Your r is a vector. You are passing that in as the first parameter of g, where it is known as x.
In g you have x/(an expression in x) . That is a matrix-right-divide request of a 1 x 101 by a 1 x 101 . Matrix right divide produces a result with a number of rows equal to the number of rows in the numerator (1 in this case) and number of columns equal to the number of rows in the denominator (1 in this case), so you get a 1 x 1 result.
Are you sure you want to do a fitting process? Would it perhaps make more sense to use element-by-element division, ./ instead of / ?

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by