# Is it possible to create a modular equation solver?

2 views (last 30 days)
Charlie Rideout on 13 Jan 2022
Edited: John D'Errico on 13 Jan 2022
Hi,
I'm pretty new to matlab and still learning a lot of the basics. I have some exams coming up and figured I could make a thermodynamics equation solver for them, as a break from revision.
From what I've been able to find, I think the best way I can do this is to have all the equations I'd like to do for as seperate functions on differnent .m files that I could then call on (althought please correct me if there is a better way to do this). However, I cannot find anything on how I might be able to make the functions work as I intend.
When I call the function, I would like to input which variable I am solving for, then input the other variables accordingly, and recieve an output for teh correct variable. I've been researching matlab code for ages and am at a complete loss. Any help would be really appriciated!

John D'Errico on 13 Jan 2022
Edited: John D'Errico on 13 Jan 2022
Is it possible? Um. sure. Virtually anything is possible. But having lots of things in separate m-files is a bad idea here. Now you need to deal with them as m-files. How would you do the solve? You would be forced to use a tool like fsolve. And then deal with issues like starting values, lists of m-file names, some interactive tools to collect them together as needed, etc. I think this is way beyond your depth from what you have said. Heck, I would not do it myself, not for money or for fun.
Can you do something similar susing symbolic tools? Now all you need to do is have a list of equations, with the unknown parameters as symbolic values. The equations can be stored together in one simple structure, easy to access, easy to work with. Decide which of those equations are necessary, given what you need to do. Now use a tool like solve. It does not need to worry about starting values, as long as a solution exists.
That said, the devil is in the details. And these details can become significant. (Honestly, I doubt it is worth the effort.) For example, consider a simple equation of the form:
syms x y
eq = y == x^3;
solve(subs(eq,y,3))
ans =
Suppose that is one of your equations, where y is given? Simple. The problem is now there are THREE solutions, TWO of which are complex. In a more complicated problem, you may conceivably have dozens of solutions, some complex, some meaningless in context of the problem you need to solve. In many problems, there will be no analytical solution available, so solve will try to pass it onto vapsolve. But vpasolve needs starting values, and it may find a solution that is not applicable to your problem, since it uses default starting values that need not be right.
In general, your knowledge of the set of equations that applies to your general topic is valuable. It quickly tells you what equation is best used for any given problem. And when that knowledge is insufficient, you will manage to grow your abilities once you do learn how to solve the problem. That is just part of learning the subject. Mastery takes practice, experience, and good judgment, plus some other things I've left out of the mix.
So if you want, go for it. I'd use the symbolic approach I discussed. Expect it to be far less good than your own internal knowledge base can achieve, and by the time you could ever possibly make the tool work in some semi-viable way, you will be onto other more interesting things, and not want to bother with it.

R2021b

### Community Treasure Hunt

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

Start Hunting!