Below is my code and I get the corresponding error message. I'm trying to use fzero on the function defined below. What am I doing wrong?
Show older comments
function [ y ] = mainFunc( v )
d = 0.15; % Diameter of sphere, meters
g = 9.8; % acceleration due to gravitym m/s^2
m = 0.5; % mass of sphere in kg
P = 101300; % N/m^2
R = 287; %gas cosntant
% Viscosisty constants b1 = 2.156954157e-14;
b2 = -5.332634033e-11;
b3 = 7.477905983e-8;
b4 = 2.527878788e-7;
T = linspace(213,333, 241); %temp in Kelvin
rho = 0*T;
u = 0*T;
for i = 1:length(T)
u(i) = b1*T(i)^3 + b2*T(i)^2 + b3*T(i) + b4;
end
for i = 1:length(T)
rho(i) = P / (R*T(i));
end
y = (24./(rho.*v*d./u) + 6./(1 + sqrt.(rho.*v*d./u)) + 0.4 - m*g) * (1/2) .* rho * v^2 * ((pi*d^2)/4) ;
end
ERROR MESSAGE >
HW5
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==> mainFunc failed with the error below.
Argument to dynamic structure reference must evaluate to a valid field name.
Error in HW5 (line 3) z = fzero(fun,x0)
Accepted Answer
More Answers (1)
Ali
on 5 Nov 2014
1 Comment
John D'Errico
on 7 Nov 2014
There is no need to add answers. Use the comments. You can post code in them too.
fzero expects functions to have scalar output, given scalar input. What do you expect it to do when your function returns an array of non-scalar shape?
>> size(y)
ans =
1 241
I have no idea what it is you ar trying to do, but I expect that you need to think about the problem, or explain clearly what it is you are trying to do.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!