Homework Help using fsolve
Show older comments
I'm trying to solve a multivariable equation with "fsolve" when given x values.
% equation given y = x^3 - y^(1/2)
% given x values and need to find y values
% this is what I'm typing into Matlab
func = @(x,y) x.^3 -y.^(1/2) -y;
x = [0,1,2.3611,2.9033,3.2859,3.5915];
Y = fsolve(func,x)
% What am I doing wrong?
Answers (2)
Star Strider
on 8 Sep 2019
I am not certain what you want to do.
Try this slight variation on your code:
Y = fsolve(@(y)func(x,y),x)
It runs without error. I defer to you to determine if it produces the correct result.
Walter Roberson
on 8 Sep 2019
fsolve() expects a function with one parameter -- not two such as x and y, but the one parameter can be a vector like x(1), x(2)
The second parameter to fsolve is the initial guess location, and in your case is a vector of 6 values. That would tell fsolve to expect to vary 6 values in vector X to try to find all(func(X) == 0)
You need to take a different strategy:
func = @(x,y) x^3 - y^(1/2) - y;
loop over elements of x using index K
y(K) = fsolve( @(y) func(x(K), y), InitialGuess )
2 Comments
Walter Roberson
on 8 Sep 2019
Ah, if you modify to
func = @(x,y) x.^3 - y.^(1/2) - y;
then
fsolve(@(y) func(x,y), x)
works on the entire vector at the same time.
aries278
on 8 Sep 2019
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!