Creating a Matrix of functions
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
KSSV
on 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
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!