# Basic Mathematics (on MATLAB & Simulink)

I need some help for the below equation, is there any function which could help me to solve for b variable in MATLAB & Simulink? Otherwise, which numerical methods would you prefer?

$$\frac{a-b}{2 R}=2 I \sinh(\frac{a+b}{2 V})$$

(assume that $a$, $R$, $I$ and $V$ is known)

Thanks.

Use fzero in matlab. Couple this with writing your function as $$f(b) = 2I\sinh\left(\frac{a+b}{2V}\right)-\frac{a-b}{2R}$$ and use the example in the link.

fminsearch is a very general optimization function which you could use to minimize the error, i.e. to minimize $|f(b) - 0|$. If a solution exists, then that norm expression should be 0. It is a very useful function if you have a small and simple problem you need a fast solution to.

However, as your function is differentiable with respect to the variables, you may want to use a numerical scheme like fzero that Chinny84 proposed above.

Both of these functions you need to know how to pass on functions to other functions:

f = @(b) 2*I*sinh((a+b)/(2*V)) - (a-b)/(2*R);

Then you pass it on as :

b_solution = fzero(f,$b_0$);

• +1. Do you mean differentiable for all variables in the equation or the variable you are trying to find the root for? I am honest in that I did not know that was a criterion and I have worked on this area. – Chinny84 Aug 13 '15 at 13:58
• fzero only "knows" about the symbols in $b$. Everything else should be hidden for it as it is "encapsulated" into the function $f$. If you check "optimset", you see that you can pass options regarding gradient and jacobians to the algorithm, so it would be reasonable to believe it's inner workings can use those things. – mathreadler Aug 13 '15 at 14:56
• Although I know the $fminsearch$ uses Melder-Nead which is a non-differentiable method. Will likely be slower for most smooth functions, but could perform better on non-smooth functions. – mathreadler Aug 13 '15 at 15:07

fun = @(b)2*sinh((0.5+b))-((0.5-b)/(2*1000));

x0 = [0.5];

x = fzero(fun,x0);

It works on MATLAB, thank you Chinny84, however, I need to use this (or any alternative) function on Simulink, but MATLAB says: "Code generation does not support anonymous functions.". Is there any way to use this function on Simulink?

I do not need to code generate, so I use "coder.extrinsic" but it does not work (I might have done something wrong). Should I solve this equation via Trapezoidal Rule or something like this, or is there any supported function?