Function will accept scalars but not vectors.
5 views (last 30 days)
Show older comments
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 Comment
per isakson
on 5 Sep 2015
Edited: per isakson
on 5 Sep 2015
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
Answers (1)
Star Strider
on 5 Sep 2015
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 Comments
per isakson
on 5 Sep 2015
"However, this still does not work."   What does the error message say now?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!