Using fminsearch in a for loop
Show older comments
I'm trying to create a stress-strain curve in matlab for 2 springs in series, one linear and one non-linear. I have my equation for the energy and have it as a function of the midpoint between the two springs. Fminsearch is used to optimize the springs such that I get the midpoint at which the energy is the smallest. I have already gotten this to work with 2 non-linear springs and 2 linear springs at a set change in length. The problem is I want to do this over a range of lengths from 0:0.20:3.80 in a for loop. When I try to do the same process in the loop, my midpoint [mp] is not changing and my forces are not correct. What mistake am I making in the loop? To me, it looks like I am calling my Energy function every iteration and it should recalculate mp via fminsearch each time with a new total length, Ltot, and yet my midpoint does not change.
a1 = 1; a2 = 1; b2 = 0.1; c2 = 1; mp_0=1;
L=0:0.20:3.80;
F1 = zeros(size(L));
F2 = zeros(size(L));
mp = zeros(size(L));
Energy= @(mp)1/2*(a1*(mp-1).^2+a2*(Ltot-mp-1).^2)+1/3*b2*(Ltot-mp-1).^3+1/4*c2*(Ltot-mp-1).^4;
for k = 1:length(L)
Ltot=L(k);disp(Ltot)
[mp]=fminsearch(Energy,mp_0);
F1(k)=a1*(mp-1);
F2(k)=(a2+b2+c2)*(Ltot-mp-1);
Ftot(k)=F1(k)+F2(k);
end
hold on
plot(L,F1);
plot(L,F2,'r');
plot(L,Ftot,'g');
Accepted Answer
More Answers (0)
Categories
Find more on Stress and Strain 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!