Back-calculating a variable in equation to give a value of zero

1 次查看(过去 30 天)
I have a non linear equation with many variables that run through a column array. I am trying to backcalulate the value of one of the variable that would produce a value of zero at each output of the column array. I am using root2d function, which is working fine, but I am only getting the last value. I am not sure how to save the output at each increment of j because the function runs througha for loop and doesn't stop till the last value. I would like the output of my variable rd to be spit out as a column or row array for each increment. Here is what I am doing:
%Calling the function
fun =@root2d;
rd0 = [0.05];
rd = fsolve(fun,rd0)
%loading input and defining variables
function F=root2d(rd)
load 'S71'
depr1=S71.Depth;
qdynr=S71.qdyn;
Velr=S71.velr;
dec2r=S71.dec;
phicv=34;
Q=6;
R=1;
V50=1;
Nkt=12;
c0=300;
c1=0.46;
c2=2.96;
gammap=8; %in kN/m3
k0=0.5;
chmin=0.031;
%for loop inside the function
for j=9:numel (depr1)
pm(j)=gammap*((1+2*k0)/3)*depr1(j);
V(j)=(Velr(j)*0.04375)/chmin;
strainterm(j)=1/(1+(V(j)/V50));
pm (j)
strainterm(j)
V(j)
qdynr(j)
F= qdynr(j)-(((Nkt*0.5*((6*sin(phicv))/(3-sin(phicv)))*exp(Q-1/rd)))+strainterm (j)*((((c0*(pm(j)^c1)*exp(rd*c2))-(Nkt*0.5*((6*sin(phicv))/(3-sin(phicv))))*exp(Q-1/rd)))));
end
end

采纳的回答

Paresh yeole
Paresh yeole 2020-7-9
编辑:Paresh yeole 2020-7-9
k = 1; % outside the loop
% inside the loop
F(k) = ... %rest remains unchanged
k = k+1;
  3 个评论
Paresh yeole
Paresh yeole 2020-7-9
The whole point of answering almost the same thing was to put the point that instead of using a cell array, try using an array..
And yes you are correct, preallocating is a good idea.
Reem Jaber
Reem Jaber 2020-7-9
Well, thanks guys! I have tried doing that before but it didn't work. Once I index F with k, the solution doesn't work anymore. I have attached the data file for your reference.

请先登录,再进行评论。

更多回答(1 个)

madhan ravi
madhan ravi 2020-7-9
% outside the loop
F = cell(9:numel(depr1), 1);
k = 0;
% inside the loop
k = k + 1; % the first line
F{k} = ... rest remains unchanged
  2 个评论
Reem Jaber
Reem Jaber 2020-7-9
Thank you! But it doesn't seem to be working, I think the problem is in F{k), it is resulting in an error in the fsolve: "FSOLVE requires all values returned by functions to be of data type double". Also, the size of the cell is resulting in an error too once I define it as you did, it worked when I changed into a scalar.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by