Solve the problem of using parameters in the solve function of MATLAB

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.