- Technical Services and Consulting
- Embedded Systems
- Electrical and Electronics Engineering
My gradient function wont update in my for loop
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Hassaan
2024-1-3
编辑:Hassaan
2024-1-3
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
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
3 个评论
更多回答(1 个)
Steven Lord
2024-1-3
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)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!