Solving a nonlinear equation

조회 수: 3 (최근 30일)
Frederik Bjerregaard
Frederik Bjerregaard 2021년 12월 8일
편집: John D'Errico 2021년 12월 8일
Hi. I have to solve a nonlinear equation over a range of values of a given parameter.
clc; clear;close all;
f = 0.35+0.1;
x0=f^2
r = 0.3:0.3:1.5;
zeta = 0.25;
eps = 1/6;
eqn= @(A)(r.^4.*A.^2)-(2.*r.^2.*A.^2)-(2.*r.^2.*((3.*eps.*A.^4)./4))+(3.*((3.*eps.*A.^4)./4))+(((9.*eps.^2.*A.^6)./16))+(4.*zeta.*r.^2.*A.^2);
Ans=fzero(eqn,x0);
I want the answer to give me a vector for A at the given value for r, but I can't get the code to work.

채택된 답변

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2021년 12월 8일
the eqn as you defined is a vector-value multi-variable function. fzero takes a scalar-value single-variable function.
so maybe you have a Writing error for eqn that make it vector value.
f = 0.35+0.1;
x0=f^2;
r = 0.3:0.3:1.5;
zeta = 0.25;
eps = 1/6;
eqn= @(A)(r.^4.*A.^2)-(2.*r.^2.*A.^2)-(2.*r.^2.*((3.*eps.*A.^4)./4))+(3.*((3.*eps.*A.^4)./4))+(((9.*eps.^2.*A.^6)./16))+(4.*zeta.*r.^2.*A.^2);
eqn(rand(1,5))
ans = 1×5
-0.0047 -0.0155 -0.0149 0.0471 0.1364
if you really want to solve the above system of equations you can use minimization functions like fminunc. or use solve for algebraic equations. ( you should change eqn for both of them)
for using fminunc the cost function should be a scalar value function. one easy method to change your eqn is taking norm from function:
eqn_= @(A)norm((r.^4.*A.^2)-(2.*r.^2.*A.^2)-(2.*r.^2.*((3.*eps.*A.^4)./4))+(3.*((3.*eps.*A.^4)./4))+(((9.*eps.^2.*A.^6)./16))+(4.*zeta.*r.^2.*A.^2));
x = fminunc(eqn_,ones(1,5));
Local minimum possible. fminunc stopped because it cannot decrease the objective function along the current search direction.
x
x = 1×5
0.4796 0.8806 0.9109 -0.0000 -0.0000
Y=eqn(x)
Y = 1×5
1.0e+-8 * -0.0519 -0.1379 -0.0523 0.0632 0.0381
norm(eqn(x))
ans = 1.7290e-09
  댓글 수: 1
John D'Errico
John D'Errico 2021년 12월 8일
편집: John D'Errico 2021년 12월 8일
Is there a valid reason why you posted the same answer twice? My guess is the site was moving slow, so you reposted it. I deleted the first of your identical answers.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Optimization에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by