Creating a Matrix of functions

1 visualizzazione (ultimi 30 giorni)
Eli Wolkenstein
Eli Wolkenstein il 29 Mar 2022
Modificato: KSSV il 29 Mar 2022
I'm trying to create a matrix with functions as its elements (in this case a 2x1 matrix of nonlinear equations) so I can create a loop where the matrix repeatedly solves the equations. However, I keep getting errors.
syms x y
f1=1/2*sin(x*y)-y/4/pi-x/2;
f2=(1-1/4/pi)*(exp(2*x)-exp(1))+exp(1).*y/pi-2*exp(1).*x;
x=.49;
y=3;
F=[f1;f2];
J=[(cos(x*y)-1)/2 ((cos(x*y))/2)-(1/(4*pi));
(-2*exp(1))+((4*pi-1)/2*pi)*exp(2*x) exp(1)/pi];
iter=0;
while(1)
XY=[x;y]-inv(J)*F(x,y);
ea=abs((xn-x)/xn)*100;
if ea<2.220446049250313e-16, break, end
x=xn;
y=yn;
XY=[xn;yn];
iter=iter+1;
end

Risposte (1)

KSSV
KSSV il 29 Mar 2022
You have to modify your code line hsown below:
x=.49;
y=3;
f1=1/2*sin(x*y)-y/4/pi-x/2;
f2=(1-1/4/pi)*(exp(2*x)-exp(1))+exp(1).*y/pi-2*exp(1).*x;
F=[f1;f2];
J=[(cos(x*y)-1)/2 ((cos(x*y))/2)-(1/(4*pi));
(-2*exp(1))+((4*pi-1)/2*pi)*exp(2*x) exp(1)/pi];
iter=0;
tol = 2.220446049250313e-5 ;
while(1)
XY=[x;y]-J\F;
xn = XY(1) ;
yn = XY(2) ;
ea=abs((xn-x)/xn)*100;
if ea<tol, break, end
x=xn;
y=yn;
XY=[xn;yn];
iter=iter+1;
end

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by