My gradient function wont update in my for loop
Show older comments
Hello!
Im trying to use Newtons method for two gradient function dU/dx= f1(x,y) and dU/dy=f2(x,y) of the function U(x,y) and find where they cross eachother for when they are zero, f1(x,y)=f2(x,y)=0, i have that code and it works!
However, i would also like to change a variabel f for one of the gradient functions and run Newtons method for each new value of f:
f2=@(x,y) E * A *( 1/L - (1./Lm1(x,y) )).*(y-y1) + E * A *( 1/L - 1./Lm2(x,y)).*(y-y2) - E * A * f ;
So, i will use values between 0 to 1 for the variable f and run the Newtons method for each f som im bascially changing the gradient function f2(x,y) for each new value of f but im doing something wrong because its like its running with f=0 for the whole loop. I havent been using matlab for a while and cant wrap my head around what im doing wrong. Here is my code and thanks alot in advance:
%Test1001
clear all
clc
clf
f=0.0;
h=3;E=1;A=1;
L=sqrt(1+h^2) ; x1=-1; y1=h; x2=1; y2=h; x3=0; y3=0;
Lm1=@(x,y)sqrt((x-x1).^2+(y-y1).^2);
Lm2=@(x,y)sqrt((x-x2).^2+(y-y2).^2);
U=@(x, y) (E*A/2*L) * ( ( Lm1(x,y) - L) ).^2 + (E*A/2*L) * ( ( Lm2(x,y) - L) ).^2 - E*A*f*(y-y3); % Potential energy function
f1=@(x,y) E*A*( 1/L - (1./Lm1(x,y) )).*(x-x1) + E*A*( 1/L - 1./Lm2(x,y)).*(x-x2) ; % Governing equations = 0
f2=@(x,y) E*A*( 1/L - (1./Lm1(x,y) )).*(y-y1) + E*A*( 1/L - 1./Lm2(x,y)).*(y-y2) - E*A*f ;
A11=@(x) 1/L - (x(2)-y1).^2 ./ (Lm1(x(1),x(2)).^3) ;
A12=@(x) (x(1)-x1).*(x(2)-y1) ./ (Lm1(x(1),x(2)).^3) ;
A21=@(x) (x(1)-x1).*(x(2)-y1) ./ (Lm1(x(1),x(2)).^3) ;
A22=@(x) 1/L - (x(1)-x1).^2 ./ (Lm1(x(1),x(2)).^3) ;
B11=@(x) 1/L - (x(2)-y2).^2 ./ (Lm2(x(1), x(2)).^3) ;
B12=@(x) (x(1)-x2).*(x(2)-y2) ./ (Lm2(x(1), x(2)).^3) ;
B21=@(x) (x(1)-x2).*(x(2)-y2) ./ (Lm2(x(1), x(2)).^3) ;
B22=@(x) 1/L - (x(1)-x2).^2 ./ (Lm2(x(1), x(2)).^3) ;
A=@(x)[A11(x) A12(x);A21(x) A22(x)];
B=@(x)[B11(x) B12(x);B21(x) B22(x)];
Df=@(x) A(x) + B(x); % The jacobian matrix
% Find roots with Newtons method
fv=@(x)[f1(x(1),x(2)) % Function vector
f2(x(1),x(2))];
for f=0:0.05:1
x=[0.1;0.10];
kmax=10; tol=0.5e-8;
for k=1:kmax
d=-Df(x)\fv(x);
x=x+d;
disp([x' norm(d)])
if norm(d)<tol, break, end
end
end
Accepted Answer
More Answers (1)
Changing the value of a variable included in an anonymous function when it was created does not change the value the anonymous function "remembers".
n = 2;
f = @(x) n*x;
f(1:5)
n = 3;
f(1:5)
You can see this "remembered" value using the functions function. To anticipate your next question no, you cannot modify the remembered value. See the "Variables in the Expression" section on this documentation page for a description of this "remembering" behavior.
info = functions(f);
info.workspace{1}
Either recreate the function handle in the loop so it "remembers" the new value of your variable or define the function handle so it accepts the value of that variable as an additional input argument and call it with the loop variable as that additional input argument.
g = @(x, n) x*n;
g(1:5, 2)
g(1:5, 3)
Categories
Find more on Numerical Integration and Differentiation 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!