How to solve a single nonlinear equation with parameter variations?

1 view (last 30 days)
#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

Accepted Answer

VBBV
VBBV on 7 Nov 2021
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.

More Answers (1)

KSSV
KSSV on 30 Jan 2021
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)

Community Treasure Hunt

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

Start Hunting!