Trying to calculate the height but get an error

I´m trying to calculate the h1 in from the equations A1 and A2 where i want to say that they are both equal to each other but i get an error saying:
Undefined function 'eq' for input arguments of type
'function_handle'
on the third line on code, is it wrong to say A1==A2? or how should i write it to make sure it take the two equations equal to each other?
A1 = @(h1) (H + b*tan(OS))- sqrt((H+b*tan(OS)^2 - h1));
A2 = @(h1) (((h0-h1)*tan(IS))*(ln(H/(H-h1))))/(tan(OS));
fcn = @(h1) A1==A2;
h1 = fzero(fcn,1)

Answers (1)

You're trying to solve for an h such that A1(h) = A2(h)? If rewrite that slightly to be in the form given in the documentation for fzero we want an h such that A1(h)-A2(h) = 0.
Your fcn doesn't evaluate the left side of that equation. So let's call fzero on that left side.
fcn = @(h1) A1(h1)-A2(h1);
h1 = fzero(fcn, 1)
Now you can check the answer in two different ways.
fcn(h1) % Should be close to 0
[A1(h1); A2(h1)] % First and second rows should be close to the same number
Arithmetic operators (like +) and the relational operators (like ==) aren't defined for function handles. Generally speaking, if you want to add or compare (etc) function handles you should add or compare (etc) the values obtained by evaluating those function handles instead.

1 Comment

I did like you said and I dont get an error anymore but I tried another similar code:
T =@(y) 10 + 2*y;
A1 = @(y) ((b + T(y))*y)/2;
SL = @(y) sqrt(y^2 + (2*y)^2);
P = @(y) b + 2*SL(y);
R = @(y) A1(y)/P(y);
Q1 = @(y) 1/n * R(y)^(2/3)*(i1)^(1/2)*A1(y)
fcn = @(y) Q(y) - ((1/n) * R(y)^(2/3)*(i1)^(1/2)*A1(y));
y = fzero(fcn,1)
I now get an error saying:
Array indices must be positive integers or logical values.
Error in fzero (line 363)
a = x - dx; fa = FunFcn(a,varargin{:});
I think this error is strange because if I do the calculations by hand it doesnt give me negative values. (P.S values n, Q and i1 are given values)

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 21 Apr 2020

Commented:

on 21 Apr 2020

Community Treasure Hunt

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

Start Hunting!