Using fsolve for a set of complex equations
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear all,
I am now using 'fsolve' to solve a set of nonlinear equations. The equations are very complex and it is difficult to directly write it out. So I hope to firstly assemble it.
dx2 = x2(n)-x(1);
dx1 = x(1);
K1 = k1*(sqrt(2)*(1-dx1)./(dx1.*sqrt((1-dx1).^2+1))-1./dx1+1);
K2 = k2*(sqrt(2)*(1-dx2)./(dx2.*sqrt((1-dx2).^2+1))-1./dx2+1);
K_step = [ K2, -K2;
-K2, K1+K2];
X_step = [x2(n);x(1)];
F_step = [x(2);0];
Then the function to be solved is
fun = @(x) K_step*X_step-F_step;
So the variables appear before the fun @(x). How can I write my codes in this condition? Thank you so much.
0 commentaires
Réponses (1)
Torsten
le 2 Avr 2023
Modifié(e) : Torsten
le 2 Avr 2023
If you want to define K_step*X_step-F_step as a function handle, you will have to define all expressions involving components of x as function handles.
dx2 = @(x)x2(n)-x(1);
dx1 = @(x)x(1);
K1 = @(x)k1*(sqrt(2)*(1-dx1(x))./(dx1(x).*sqrt((1-dx1(x)).^2+1))-1./dx1(x)+1);
K2 = @(x)k2*(sqrt(2)*(1-dx2(x))./(dx2(x).*sqrt((1-dx2(x)).^2+1))-1./dx2(x)+1);
K_step = @(x)[ K2(x), -K2(x);
-K2(x), K1(x)+K2(x)];
X_step = @(x)[x2(n);x(1)];
F_step = @(x)[x(2);0];
fun = @(x)K_step(x)*X_step(x)-F_step(x);
This looks (and is) very ugly.
So I suggest you define a function to compute K_step*X_step-F_step:
fun = @(x)xstep(x,x2,n,k1,k2)
function res = xstep(x,x2,n,k1,k2)
dx2 = x2(n)-x(1);
dx1 = x(1);
K1 = k1*(sqrt(2)*(1-dx1)./(dx1.*sqrt((1-dx1).^2+1))-1./dx1+1);
K2 = k2*(sqrt(2)*(1-dx2)./(dx2.*sqrt((1-dx2).^2+1))-1./dx2+1);
K_step = [ K2, -K2;
-K2, K1+K2];
X_step = [x2(n);x(1)];
F_step = [x(2);0];
res = K_step*X_step-F_step;
end
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!