fsolve in a for loop

2 views (last 30 days)
Rajeev kamal
Rajeev kamal on 18 Dec 2015
Commented: Star Strider on 21 Jan 2016
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.

Answers (1)

Rajeev kamal
Rajeev kamal on 19 Jan 2016
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 Comments
Rajeev kamal
Rajeev kamal on 21 Jan 2016
Finally the loop has worked although I am getting exitflag 2 after each iteration. Don't know the significance yet.
Star Strider
Star Strider on 21 Jan 2016
See my Answer to your Question What does exitflag =2 mean for fsolve.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!