Homework Help using fsolve
이전 댓글 표시
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?
답변 (2개)
Star Strider
2019년 9월 8일
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
2019년 9월 8일
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
Walter Roberson
2019년 9월 8일
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
2019년 9월 8일
카테고리
도움말 센터 및 File Exchange에서 Numeric Solvers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!