fsolve in a for loop

조회 수: 2 (최근 30일)
Rajeev kamal
Rajeev kamal 2015년 12월 18일
댓글: Star Strider 2016년 1월 21일
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.
Star Strider
Star Strider 2016년 1월 21일
See my Answer to your Question What does exitflag =2 mean for fsolve.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by