Error in fzero usage

Hi! MATLAB Query: I have written an exercise in m file for some design parameters of a heat exchangers. On running the m file, the error message reads as below in the command window. Can someone help as to what is wrong? Thanks.
quote ??? Error using ==> fzero at 317 FZERO cannot continue because user supplied function_handle ==> T2HotSide failed with the error below.
The number of sites, 8, is incompatible with the number of values, 7.
Error in ==> HeatExchanger at 12 Th2=fzero(@T2HotSide,Th1+15,options); unquote

Answers (4)

Fangjun Jiang
Fangjun Jiang on 8 Aug 2011

0 votes

"The number of sites, 8, is incompatible with the number of values, 7." This message seems to be an error message from that particular function T2HotSide(). Sounds to me that that there are 8 sites specified but only 7 valules are provided.
Searching for the phrase "The number of sites", or "is incompatible with the number of values" in T2HotSide.m might help you to find the where the error occurred. Also, put a break point in T2HotSide.m and step through will pinpoint exactly which line the error occurred.
Walter Roberson
Walter Roberson on 8 Aug 2011

0 votes

Is Th1 indeed a vector with at least 8 values?
Hi Fangjung & Walter!
  1. Th1 has only one value (it is the temperature of the hot fluid at entry).
  2. The sub function I have written is copied below. In the script, the value of cph is dependent on (Th1+Th2)/2.Could that be causing the error as in the last equation ("Th=...")there are 8 variables??? For all water properties, 8 temperature inputs have been considered.
My objective : To find Th2 for which the equation Th becomes zero.
Th2=fzero(@T2HotSide,Th1+15,options);
function Th = T2HotSide(Th2)
global Tc1 Tc2 Th1 ms mt
cph=WaterProperties((Th2+Th1)/2);
cpc=WaterProperties((Tc1+Tc2)/2);
Th=Th1-mt*cpc*(Tc2-Tc1)/ms/cph-Th2;

4 Comments

What is WaterProperties? And what are the size and shape of the other variables?
If any of your variables are vectors or matrices, make sure you use .* and ./ instead of * and / or else you will get different results than you expect.
I suggest you put a breakpoint in T2HotSide and single-step through it and see where it creates an error. There isn't anything _obvious_.
Note: it is considered better to use anonymous functions to pass in additional variables to fzero, rather than to use global variables.
1. water properties are a set of specific heat values (8 values at 8 different temperatures)cph,cph=specific heat of hot fluid, cold fluid.
2. The other variables have fixed values Th1=65 Tc1=18 Tc2=42(temperatures) ms=14 mt=8.5 (flow rates).
3. please advise how I actually put a breakpoint & single step. I have tried with instructions from help file to no avail.
Tq
To check: each call to WaterProperties returns a vector of 8 values? If so then your Th expression would be calculating based upon two vectors, but the expression needs to return a single value out of T2HotSide . As the constants (Tc2-Tc1) and ms could be moved in the expression, if cph and cpc are indeed vectors, you would effectively have cph / cpc with the "/" being the mrdivide operator, which would be more or less cph * pinv(cpc) with the * indicating matrix multiplication. Are you sure that is what you want?
Hi!
I have introduced breaks and tried to debug but I could not understand the long file statements. I ahve tried some options but it still does not work and returns the same error..."8 sites....7 values.."
One thing appears certain that the error lies in Th=@T2HotSide,Th1+15,options;
Is there any way I can forward the file to you? I am very new to MATLAB but trying...Would appreciate your patience...

Sign in to comment.

Fangjun Jiang
Fangjun Jiang on 8 Aug 2011

0 votes

Watch this video: http://blogs.mathworks.com/videos/2010/09/02/using-debugger-to-walk-through-code/ for basic code debugging techniques. Many problem can be easily solved if you know how to step and observe the value of the variables.

1 Comment

Hi!
Got that & I have introduced breaks and tried to debug but I could not understand the long file statements. I ahve tried some options but it still does not work and returns the same error..."8 sites....7 values.."
One thing appears certain that the error lies in Th=@T2HotSide,Th1+15,options;
Is there any way I can forward the file to you? I am very new to MATLAB but trying...Would appreciate your patience...

Sign in to comment.

Categories

Products

Asked:

on 8 Aug 2011

Community Treasure Hunt

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

Start Hunting!