MATLAB Answers

variable needs to be array but only returning 1 value

55 views (last 30 days)
Benjamin
Benjamin on 28 Mar 2019
Commented: Stephen Cobeldick on 28 Mar 2019
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

  0 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 28 Mar 2019
Edited: Walter Roberson on 28 Mar 2019
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 / ?

Community Treasure Hunt

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

Start Hunting!

Translated by