When we use the solve function of MATLAB, we often encounter this situation, that is, the equation contains some known parameters to control, but how to get the numerical solution. Examples are as follows:
var1=100
To solve this equation, if you write directly, var1*x^2+20*x=0
Solve (' var1*x^2+20*x=0 ', ' x ')
is not able to get the correct solution.
The output is:
Ans =
[0]
[ -20/VAR1]
Not the result we want.
Take a closer look at Sovle's instructions and find that the first argument is a string, and the parameter var1 is a number. What needs to be done is to combine the value of var1 with a string into a string and then solve it.
If the direct connection is used: [Var1, ' *x^2+20*x=0 '] also not, because the var1 is a number, you cannot complete the merge, you need to convert it to a string.
Fortunately there is the NUM2STR function, the workaround is as follows:
var1=100;
Solve ([Num2str (VAR1), ' *x^2+20*x=0 '], ' x ')
The output is:
Ans =
[0]
[ -1/5]