Function to solve an equation for a given variable

1 view (last 30 days)
I would like to have a function like f(a,b,c,d,e) which I could call multiple times in my code but to be solved for different variables:
ans1 = f(X,b,c,d,e) it solves for the first variable, I set the other 4
ans2=f(a,b, X, d, e) solves for the third, I set the other 4
and so on.

Accepted Answer

John D'Errico
John D'Errico on 22 Nov 2022
Edited: John D'Errico on 22 Nov 2022
If it is symbolic problem, then it is trivial. Just assign those variables, subs them in, and use solve. WTP?
If it is a numerical problem, then just create a function handle with the one variable as an unknown.
In either case, it is trivial. So WTP?
Could you get fancier, and write a wrapper function, where you pass it in the variables you want to assign, and tell it which variable is to be solved for? Yes. Go for it. Write the code, then post it on the File Exchange. If you manage to do so with a wonderfully clean interface, others will find it useful. If you can't write that clean interface, then don't waste your time, as the above solutions are so easy to implement. And since I and others have never seen a reason to bother to come up with a nice function to do something like this that I know of, it suggests that many other authors agree with me. But go for it.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!