Hey everyone, long time no post :). It’s also been some time since I have been working in Matlab, but today I found enough time to waste. I am working on a UNIQUAC model for predicting binary solution activities, and also have been trying to generate a X-Y plot for distillation. I ran into a problem however, I have a function:

%Uniquac.m function gamma = Uniquac(r_matrix, q_matrix, a_matrix, T, x1) ... (More code here)

The extra parameters in the function above are really nice, I might have to call several different binary components with the same function. While I could hard-code the constants r_matrix, q_matrix, and a_matrix… I really feel cheap doing so. Why does it matter so much? I don’t want to corrode my perfectly general code with very specific constants. Consider the very real possibility that you might want to use this function in fsolve, a numeric solver in Matlab. Fsolve requires:

1. That the first input be a function.

2. The second input must be a ‘guess’ for a possible solution.

The problem is that when you use a function like Uniquac.m, fsolve(@Uniquac, x0) says to Matlab: “Use the matrix x0 as the starting point for meeting my constraints.” But what is x0?

Well, it’s the inputs of Uniquac.m… which are r_matrix, q_matrix, a_matrix, T and x1. But hold on… maybe I only want to allow T to change and if r_matrix, q_matrix, a_matrix, and x1 are supposed to be constants, then it would be more than desirable if Matlab didn’t change them. But in this configuration Matlab cannot distinguish between the ‘constants’ and the variables that you want to change.

So what then? How to fix this problem? Well, you *could* go back and ruin all that beautiful code you just wrote by hard-coding constants and making global variables… but you could also go back to dragging your fists and beating things with a club.

OR we could do something VERY hip and gets rid of the bad vibes. For this we pull out a seemingly useless friend: the anonymous function. We covered these previously but here’s the angle:

%Some other script than Uniquac.m shortUniquac = @(T) Uniquac(r_matrix, q_matrix, a_matrix, T, x1)

Yea! Now you’ve got a good as new function shortUniquac(T) that depends only on T… without a bunch of band-aids. Now for fsolve:

%fsolver answer = fsolve(shortUniquac, T0)

If you would like the files for the Uniquac stuff, leave a comment with your email (do not put “example@gmail.com”, rather “example \ gmail” to avoid spam) and what you intend to use it for and I’ll send you a link.

——-Updated 04/23/2012———

User List

- Changsoo Lee — Yonsei Univ. Korea
- Bobby J — The University of Edinburgh
- Santiago — University of Los Andes; Bogotá, Colombia