Creating a Matrix of functions

1 次查看(过去 30 天)
Eli Wolkenstein
Eli Wolkenstein 2022-3-29
编辑: KSSV 2022-3-29
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

回答(1 个)

KSSV
KSSV 2022-3-29
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by