How to solve a single nonlinear equation with parameter variations?

조회 수: 5(최근 30일)
Xhetri Sita
Xhetri Sita 2021년 1월 30일
답변: VBBV 2021년 11월 7일
#Function file
function f=reso(A,w)
global taue k gamma hac;
f=(((taue+k.^2)-w.^2)*A+(9/2)*A.^3-(75/64)*A.^5).^2+(gamma*k*w*A).^2-hac.^2;
end
Script file
global taue k gamma hac;
taue=0.95;
L=5;
k=pi/(2*L);
hac=1.46;
gamma=0.95;
A0=0.001; %Initial guess
w=0:0.1:5;
for k= 1:length(w)
sol = fzero(@(A) reso(A,w(k)),A0);
plot(w,sol);
end
Here the for loop is not working. Could anybody please point out the problem. Thanks in advance

답변(2개)

KSSV
KSSV 2021년 1월 30일
It is suggested not to use global.
Replace these lines:
w=0:0.1:5;
for k= 1:length(w)
sol = fzero(@(A) reso(A,w(k)),A0);
plot(w,sol);
end
with
w=0:0.1:5;
sol = zeros(size(w)) ;
for k= 1:length(w)
sol(k) = fzero(@(A) reso(A,w(k)),A0);
end
plot(w,sol)

VBBV
VBBV 2021년 11월 7일
taue=0.95;
L=5;
kk=pi/(2*L); % change this to different variable
hac=1.46;
gamma=0.95;
A0=0.01; %Initial guess
w=0:0.1:5;
for k= 1:length(w)
sol = fzero(@(A) reso(A,w(k),gamma,taue,kk,hac),A0);
plot(w,sol,'bo');
hold on
end
function f=reso(A,w,gamma,taue,kk,hac)
f=(((taue+kk.^2)-w.^2)*A+(9/2)*A.^3-(75/64)*A.^5).^2+(gamma*kk*w*A).^2-hac.^2;
end
Alternately you can pass the variables as arguments in the function call.
You also seem to use the variable k both for parametric values and loop iterator, it essentially overides the value assigned to variables.

Community Treasure Hunt

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

Start Hunting!

Translated by