Recently, I had assigned a project to my class where they needed to regress *n* number of *x-y* data points to a nonlinear regression model *y*=exp(*b***x*). However, they were NOT allowed to transform the data, that is, transform data such that linear regression formulas can be used to find the constant of regression *b*. They had to do it the new-fashioned way: Find the sum of the square of the residuals and then minimize the sum with respect to the constant of regression *b*.

To do this, they conducted the following steps

- setup the equation by declaring
*b* as a syms variable,
- calculate the sum of the square of the residuals using a loop,
- use the
*diff* command to set up the equation,
- use the
*solve* command.

However, the *solve* command gave some odd answer like log(z1)/5 + (2*pi*k*i)/5. The students knew that the equation has only one real solution – this was deduced from the physics of the problem.

We did not want to set up a separate function mfile to use the numerical solvers such as *fsolve*. To circumvent the setting up of a separate function mfile, we approached it as follows. If dbsr=0 is the equation you want to solve, use

F = vectorize(inline(char(dbsr)))

fsolve(F, -2.0)

What *char* command does is to convert the function dbsr to a string, inline constructs it to an inline function, vectorize command vectorizes the formula (I do not fully understand this last part myself or whether it is needed).

### Like this:

Like Loading...

## Author: Autar Kaw

Autar Kaw (http://autarkaw.com) is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare (nm.MathForCollege.com) annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.
View all posts by Autar Kaw

## One thought on “Does the solve command in MATLAB not give you an answer?”