Function will accept scalars but not vectors.

조회 수: 8 (최근 30일)
Josh McCraney
Josh McCraney 2015년 9월 5일
편집: per isakson 2015년 9월 5일
I have a question that I have not been able to find. The below function accepts outside scalar inputs but not vectors. The code is below.
Also, a, b, delta,FA,Hi,vHnew,iv are all scalars defined outside the function. The vector I would like to add as an input is x, which I've commented below what it could be. However, even if I add this to the function line and fa, fb, and fc, along with the fx function at the end, I still receive "Error Not enough input arguments." Please help me so this function can operate on x when it is defined outside the function. Any help is greatly appreciated!
function c = mshvol(a, b, delta,FA,Hi,vHnew,iv)
fa = f(a,FA,Hi,vHnew,iv);
fb = f(b,FA,Hi,vHnew,iv);
while ( abs(b - a) > 2*delta )
c = (b + a)/2;
fc = f(c,FA,Hi,vHnew,iv);
if sign(fc) ~= sign(fb)
a = c; fa = fc;
else
b = c; fb = fc;
end% end if
end% end while
function fx = f(zbulk,FA,Hi,vHnew,iv,x)
dx=.001;
% x=0:dx:5;
y=25-x.^2;
diff = abs(x-zbulk);
[~, idx] = min(diff); %index of closest value
z=y(idx:end);
fx = zbulk*15+trapz(z)*dx-70; %%Volume balance
return;
  댓글 수: 1
per isakson
per isakson 2015년 9월 5일
편집: per isakson 2015년 9월 5일
Try
>> c = mshvol( 1, 2, 0.01,3,4,5,6,7)
c =
1.0156
and
>> c = mshvol( 1, 2, 0.01,3,4,5,6,[0:0.001:5])
c =
1.4219
where
function c = mshvol(a, b, delta,FA,Hi,vHnew,iv,x)
fa = f(a,FA,Hi,vHnew,iv,x);
fb = f(b,FA,Hi,vHnew,iv,x);
while ( abs(b - a) > 2*delta )
c = (b + a)/2;
fc = f(c,FA,Hi,vHnew,iv,x);
if sign(fc) ~= sign(fb)
a = c; fa = fc;
else
b = c; fb = fc;
end% end if
end% end while
end
function fx = f(zbulk,FA,Hi,vHnew,iv,x)
dx=.001;
% x=0:dx:5;
y=25-x.^2;
diff = abs(x-zbulk);
[~, idx] = min(diff); %index of closest value
z=y(idx:end);
fx = zbulk*15+trapz(z)*dx-70; %%Volume balance
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2015년 9월 5일
I’m not quite sure what you’re doing, and we don’t have your ‘f’ function. The solution is likely to vectorise your code. See Array vs. Matrix Operations for details.
  댓글 수: 2
Josh McCraney
Josh McCraney 2015년 9월 5일
Thanks for replying! To clarify, on the lines of code where I have fa = f(a,FA,Hi,vHnew,iv) I have changed to fa = f(a,FA,Hi,vHnew,iv,x), where I included the x. This was necessary for the scalar values a,FA,Hi,vHnew,iv. However, this still does not work.
Does this clarify the issue?
per isakson
per isakson 2015년 9월 5일
"However, this still does not work." &nbsp What does the error message say now?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by