Clear Filters
Clear Filters

My gradient function wont update in my for loop

2 views (last 30 days)
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

Hassaan
Hassaan on 3 Jan 2024
Edited: Hassaan on 3 Jan 2024
clear all
clc
clf
% Constants
h=3; E=1; Area=1;
L=sqrt(1+h^2); x1=-1; y1=h; x2=1; y2=h; x3=0; y3=0;
% Define the length functions
Lm1=@(x,y) sqrt((x-x1).^2 + (y-y1).^2);
Lm2=@(x,y) sqrt((x-x2).^2 + (y-y2).^2);
% Potential energy function
U=@(x, y) (E*Area/2*L) * ((Lm1(x,y) - L)).^2 + (E*Area/2*L) * ((Lm2(x,y) - L)).^2 - E*Area*f*(y-y3);
% Governing equations = 0 (f1 does not depend on f so we can define it outside the loop)
f1=@(x,y) E*Area*(1/L - (1./Lm1(x,y))).*(x-x1) + E*Area*(1/L - 1./Lm2(x,y)).*(x-x2);
% Define the Jacobian matrix and Function vector components outside the loop
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);
% Assemble the Jacobian matrix and Function vector
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);
% Main loop over f values
for f_value=0:0.05:1
% Redefine f2 with the current value of f
f2=@(x,y) E*Area*(1/L - (1./Lm1(x,y))).*(y-y1) + E*Area*(1/L - 1./Lm2(x,y)).*(y-y2) - E*Area*f_value;
% Initial guess
x=[0.1;0.10];
% Newton's method parameters
kmax=10; tol=0.5e-8;
for k=1:kmax
% Function vector
fv=@(x) [f1(x(1),x(2)); f2(x(1),x(2))];
% Update step
d=-Df(x)\fv(x);
x=x+d;
% Display current iteration results
disp([x' norm(d)])
if norm(d)<tol, break, end
end
end
-0.0274 -0.0025 0.1635 -0.0001 -0.0001 0.0273 1.0e-03 * -0.0000 -0.0000 0.1687 1.0e-07 * 0.0000 -0.0000 0.2646 1.0e-15 * 0.6924 -0.1745 0 -0.0030 0.0869 0.1038 -0.0000 0.0882 0.0032 -0.0000 0.0882 0.0000 -0.0000 0.0882 0.0000 0.0214 0.1764 0.1096 0.0001 0.1773 0.0213 0.0000 0.1774 0.0001 0.0000 0.1774 0.0000 -0.0000 0.1774 0.0000 0.0458 0.2658 0.1744 0.0005 0.2672 0.0453 0.0000 0.2675 0.0005 0.0000 0.2675 0.0000 -0.0000 0.2675 0.0000 0.0701 0.3552 0.2569 0.0030 0.3582 0.0672 0.0000 0.3588 0.0030 0.0000 0.3588 0.0000 -0.0000 0.3588 0.0000 0.0945 0.4446 0.3446 0.2470 0.4568 0.1529 0.0588 0.4470 0.1884 0.8145 0.4644 0.7559 89.8051 21.5534 91.4553 3.0940 4.0488 88.4603 7.7704 -7.2869 12.2625 1.9337 0.9120 10.0642 0.8409 0.2629 1.2710 0.9068 0.5626 0.3068 0.1189 0.5340 0.4344 -0.0170 0.5432 0.1362 0.0005 0.5458 0.0177 -0.0000 0.5458 0.0005 0.0000 0.5458 0.0000 0.0000 0.5458 0.0000 0.1433 0.6234 0.5252 -0.0156 0.6384 0.1595 0.0003 0.6421 0.0163 -0.0000 0.6421 0.0003 0.0000 0.6421 0.0000 0.0000 0.6421 0.0000 0.1676 0.7128 0.6165 -0.0175 0.7355 0.1865 0.0003 0.7407 0.0185 -0.0000 0.7408 0.0003 0.0000 0.7408 0.0000 0.0000 0.7408 0.0000 0.1920 0.8022 0.7082 -0.0206 0.8351 0.2152 0.0004 0.8423 0.0222 -0.0000 0.8424 0.0004 0.0000 0.8424 0.0000 -0.0000 0.8424 0.0000 0.2164 0.8916 0.8001 -0.0246 0.9380 0.2454 0.0005 0.9477 0.0269 -0.0000 0.9478 0.0005 0.0000 0.9478 0.0000 0.0000 0.9478 0.0000 0.2408 0.9810 0.8922 -0.0291 1.0449 0.2773 0.0006 1.0581 0.0325 -0.0000 1.0583 0.0006 0.0000 1.0583 0.0000 0.0000 1.0583 0.0000 0.2651 1.0704 0.9844 -0.0339 1.1573 0.3114 0.0007 1.1754 0.0390 -0.0000 1.1757 0.0008 0.0000 1.1757 0.0000 0.0000 1.1757 0.0000 0.2895 1.1598 1.0766 -0.0383 1.2771 0.3482 0.0009 1.3030 0.0469 -0.0000 1.3035 0.0010 0.0000 1.3035 0.0000 -0.0000 1.3035 0.0000 0.3139 1.2492 1.1690 -0.0416 1.4070 0.3890 0.0009 1.4477 0.0589 -0.0000 1.4491 0.0016 0.0000 1.4491 0.0000 -0.0000 1.4491 0.0000 0.3383 1.3386 1.2614 -0.0422 1.5512 0.4358 0.0003 1.6274 0.0873 0.0000 1.6346 0.0072 0.0000 1.6347 0.0001 -0.0000 1.6347 0.0000 -0.0000 1.6347 0 0.3626 1.4281 1.3538 -0.0373 1.7161 0.4928 -0.0046 1.9185 0.2050 -0.0276 4.2510 2.3326 0.2360 15.0140 10.7662 0.0516 7.4022 7.6141 0.0011 7.3469 0.0748 0.0000 7.3467 0.0011 0.0000 7.3467 0.0000 -0.0000 7.3467 0.0000 0.3870 1.5175 1.4462 -0.0217 1.9116 0.5678 -0.2594 6.7462 4.8404 0.1197 7.4493 0.7987 0.0010 7.4295 0.1203 0.0000 7.4286 0.0014 0.0000 7.4286 0.0000 -0.0000 7.4286 0.0000 0.4114 1.6069 1.5387 0.0138 2.1539 0.6762 -0.9579 1.5676 1.1349 -0.0167 1.7060 0.9513 -0.0070 2.4814 0.7754 -0.0108 2.0807 0.4007 0.0919 1.3049 0.7826 -0.0508 1.9748 0.6850 0.5123 -0.6990 2.7324 0.7919 1.5596 2.2759 0.4358 1.6963 1.6312 0.0865 2.4691 0.8481 0.1627 2.0143 0.4612 -2.9856 -2.7197 5.6853 -1.8751 1.9840 4.8330 -0.7489 0.7151 1.6966 3.0101 1.0238 3.7717 7.8054 9.5966 9.8228 2.0385 6.9595 6.3412 -0.0535 7.9443 2.3122 0.4601 1.7857 1.7237 0.2290 2.9002 1.1382 0.0961 2.2975 0.6172 0.5193 1.7289 0.7088 0.1113 2.6525 1.0098 0.1370 2.1290 0.5241 -5.8026 -0.9538 6.6920 -3.1425 3.5879 5.2634 -7.6101 -14.6201 18.7481 -1.3534 1.7319 17.5081
In this revised code, f2 is redefined for each new value of f_value in the loop, allowing the Newton's method to run for each value of f from 0 to 1 in increments of 0.05. This should fix the issue of it running as if f=0 for the entire loop.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  3 Comments
Hassaan
Hassaan on 3 Jan 2024
@Torsten Thanks for pointing out. Resolved the confilit in the above code. Thank you.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 3 Jan 2024
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)
ans = 1×5
2 4 6 8 10
n = 3;
f(1:5)
ans = 1×5
2 4 6 8 10
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}
ans = struct with fields:
n: 2
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)
ans = 1×5
2 4 6 8 10
g(1:5, 3)
ans = 1×5
3 6 9 12 15

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!