fsolve in a for loop

2 次查看(过去 30 天)
Rajeev kamal
Rajeev kamal 2015-12-18
I am trying an analytical solution from nonlinear equation for a transient temperature problem. I call the function as
fun = @funhouse;
x0 = [1,1]';
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@funhouse,x0,options);
And the function is
function F = funhouse(x,Tain)
Ts=18+273;% in K
Ca=11918;% kJ/K
Hae=52.33;%W/K
Hame=41.26;%W/K
Has=87.43;%W/K
Hag=15.54;%W/K
Htot=Hae+Hame+Has+Hag;
tao=17;%W/K
Te=10+273;
t=1;
tao=17;
%FUNHOUSE Summary of this function goes here
% Detailed explanation goes here
F = [x(1)-Ca*(x(2)-Tain)-Hae*(x(2)-Te)-Hame*(x(2)-Te)-Hag*(x(2)-Te)-Has*(x(2)-Te);
x(2)-(Tain-(x(1)/Htot)-(Htot-Has)*Te/Htot-Has*Ts/Htot)*exp(-t/tao)-(x(1)+(Htot-Has)*Te+Has*Ts)/Htot];
end
How to pass Tain as the last value found from the fsolve to the next iteration and save the output in an array.

回答(1 个)

Rajeev kamal
Rajeev kamal 2016-1-19
Modified code to fix Tenv(i) to pass to the funh.m
________________________________________
clear clc Ta=zeros(1,25); t=1; Ta(1)=16+273; Te=[9 12 10 10 18 18 11 11 11 11 12 16 16 18 18 20 20 15 15 13 13 12 12 10 9 9 12 10 10 10 10 11 11 11 11 12 16 16 18 18 20 20 15 15 13 13 12 12 10 9 9 12 10 10 10 10 11 11 11 11 12 16 16 18 18 20 20 15 15 13 13 12 12 10 9 9 12 10 10 10 10 11 11 11 11 12 16 16 18 18 20 20 15 15 13 13 12 12 10 9]'; P=ones(1,25); x0(2)=293; x0(1)=6; for i=2:100 % fun = @funh; Tain=Ta(i-1); Tenv(i)=Te(i)+273; options = optimoptions('fsolve','Display','iter') [xopt,Fval,exitflag] = fsolve(@(x)funh(x,Tain,Tenv(i)),x0,options) Ta(i) = xopt(2); % Tain=xopt(2); xsav(i,:) = xopt; end i=1:1:100; plot(i,xsav(:,1),i,xsav(:,2));
------------------- Now I am getting the Ta as almost constant for all iterations while the xsav(1) fluctuated to negative and positive values. The expected values should be positive real for xsav(1) while xsav(2) should give more fluctuation. @star strider I am not familiar with patternsearch and neither the Matlab help recognizes this.
  2 个评论
Rajeev kamal
Rajeev kamal 2016-1-21
Finally the loop has worked although I am getting exitflag 2 after each iteration. Don't know the significance yet.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by