Im running a code with fsolve inside a for-loop, code is working but not when parameters are sourced from a different file.
1 view (last 30 days)
Show older comments
Good day everyone!
So im working on a code that runs a an fsolve inside a for loop. The code is working and checks were made to make sure the output are meaningful.
My problem now is that it does not work when input parameters (Disp matrix) are taken from a different file ( Disp array flagged as comment, sourced from a different file) even when data type and values are the same as declared Disp matrix.
My code is:
clc;
%%
Disp = [1,0.122436023196500,81471588.7476144,0.0538718502064602,35847499.0489503;
2,0.276497612386024,183987515.911537,0.121658949449850,80954507.0010762;
3,0.335471943675830,223230316.682658,0.147607655217365,98221339.3403695;
4,0.337210139518519,224386949.944350,0.148372461388149,98730257.9755142;
5,0.306140645403441,203712633.831701,0.134701883977514,89633558.8859486;
6,0.260691124132727,173469535.362219,0.114704094618400,76326595.5593765;
7,0.221878189699896,147642565.899770,0.0976264034679543,64962728.9958989
];
% Disp(:,1) = input(:,1); % Story
% Disp(:,2) = double(DUp(:)); % Peak Disp DUp
% Disp(:,3) = double(Vp(:)); % Peak Shear Vp
% Disp(:,4) = double(DUy(:)); % Yield Disp DUy
% Disp(:,5) = double(Vy(:)); % Yield Shear Vy
options = optimset('TolFun',1e-16,'MaxFunEvals',1e9,'Maxiter',1e9);
options.StepTolerance = 1e-12;
%%
N = size(Disp,1);
GG = zeros(2,N);
exitflags = zeros(1,N);
for i = 1:N
O = [0,0];
tau = 0.5;
Bdispy = 0;
Cdispy = 0;
Peakdispx(i) = Disp(i,2);
Peakdispy(i) = Disp(i,3);
Adispy(i) = Peakdispy(i);
mslope(i) = (Disp(i,5)-O(2))/(Disp(i,4)-O(1));
Cdispx(i) = Peakdispx(i)-((Peakdispy(i)-Cdispy)/(mslope(i)));
Bdispx(i) = Cdispx(i)*-1;
Adispx(i) = (Adispy(i)-Bdispy)/(mslope(i))+Bdispx(i);
slopeACdisp(i) = ((Adispy(i)-Cdispy)/(Adispx(i)-Cdispx(i)));
Adispfull(i) = (0.5*(Adispx(i)*Bdispy+Bdispx(i)*Cdispy+Cdispx(i)*Peakdispy(i)+Peakdispx(i)*Adispy(i)-Adispy(i)*Bdispx(i)-Bdispy*Cdispx(i)-Cdispy*Peakdispx(i)-Peakdispy(i)*Adispx(i)));
F = @(GG)[slopeACdisp(i)-((GG(2)-Cdispy)/(GG(1)-Cdispx(i)));
-tau+(0.5*((GG(1)*Bdispy+Bdispx(i)*Cdispy+Cdispx(i)*Peakdispy(i)+Peakdispx(i)*GG(2)-GG(2)*Bdispx(i)-Bdispy*Cdispx(i)-Cdispy*Peakdispx(i)-Peakdispy(i)*GG(1))))/Adispfull(i)]
gg0 = [tau*Disp(i,4);tau*Disp(i,5)];
GG = fsolve(F,gg0);
%
%% pinch in x and y
disppinchy(i) = GG(2)/Disp(i,3);
U(i) = ((GG(2)-Cdispy)/mslope(i))+Cdispx(i);
L(i) = ((GG(2)-Bdispy)/mslope(i))+Bdispx(i);
disppinchx(i) = (GG(1)-L(i))/(U(i)-L(i));
% check, ratio of Area should be equal to tau
Gdispx(i) = GG(1);
Gdispy(i) = GG(2);
Adisppin(i) =0.5*(Gdispx(i)*Bdispy+Bdispx(i)*Cdispy+Cdispx(i)*Peakdispy(i)+Peakdispx(i)*Gdispy(i)-Gdispy(i)*Bdispx(i)-Bdispy*Cdispx(i)-Cdispy*Peakdispx(i)-Peakdispy(i)*Gdispx(i));
ratiodisp(i) = Adisppin(i)/Adispfull(i);
% check for slope of line AC and line GC
slopedisp1(i) = (Gdispy(i)-Bdispy)/(Gdispx(i)-Bdispx(i));
slopedisp2(i) = (Adispy(i)-Bdispy)/(Adispx(i)-Bdispx(i));
slopedispdiff(i) = slopedisp1(i)-slopedisp2(i); % sloperatio should be 1
end
Disp(:,7) = disppinchy(:);
Disp(:,6) = disppinchx(:);
1 Comment
Alan Weiss
on 26 Nov 2021
I'm sorry, but I do not understand your question. I do not see how you are attempting to take the values from a different file, so I do not understand what might cause the issue that you see. Please indicate exactly what the other file is, what is the path to the other file, what do you see happening. I strongly suspect that there is nothing special about running solve in a loop, but you simply have not transferred data correctly.
Furthermore, I don't think that your options make sense. Try setting
options = optimoptions('fsolve');
with no nondefault settings. The program runs the same for me either way, leading me to suspect that your nondefault option settngs are unnecessary.
Alan Weiss
MATLAB mathematical toolbox documentation
Answers (0)
See Also
Categories
Find more on Variables 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!