# Cprintf utility--great way to see disequilibria

**URL:** https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608
**Category:** Uncategorized
**Created:** [March 12, 2026, 9:41pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608 "2026-03-12T21:41:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![MichaelTiemann](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/michaeltiemann/32/220_2.png) [@MichaelTiemann](https://discourse.vfitoolkit.com/u/MichaelTiemann)
#### Post date: [March 12, 2026, 9:41pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/1 "2026-03-12T21:41:02Z")

</div>

I have discovered the ill effects of having a large-but-irrelevant GE term ruin the solution to an otherwise solvable GE system. For example in my case TargetCapitalRatio trying to equalize K/L with a target of 2.03. The fact that I’ve changed enough things to break that number means that disparity can prevent finding anything sane in the rest of the equations.

By highlighting the most unhappy general equilibrium equation, it greatly simplifies tracking what’s driving the solver. And I can do that with the free add-on `cprintf`.

 ![cprint_demo](https://discourse.vfitoolkit.com/uploads/default/original/1X/fc769167a656a6603dab4803b1b89975195406ec.png)

```auto
fprintf('Current GeneralEqmEqns: \n')
[~,maxidx]=max(GeneralEqmConditionsVec.^2);
for gg=1:length(GEeqnNames)
    if gg==maxidx
        cprintf('err', heteroagentoptions.verboseaccuracy2,GEeqnNames{gg},GeneralEqmConditionsVec(gg))
    else
        fprintf(heteroagentoptions.verboseaccuracy2, GEeqnNames{gg},GeneralEqmConditionsVec(gg))
    end
end

```

Just a thought…

---

<div class="post-metadata">

### Author: ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)
#### Post date: [March 12, 2026, 10:31pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/2 "2026-03-12T22:31:24Z")

</div>

You mean make it so that the ‘worst’ GE condn is highlighted (is in colour)?

---

<div class="post-metadata">

### Author: ![MichaelTiemann](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/michaeltiemann/32/220_2.png) [@MichaelTiemann](https://discourse.vfitoolkit.com/u/MichaelTiemann)
#### Post date: [March 12, 2026, 10:43pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/3 "2026-03-12T22:43:00Z")

</div>

Exactly. That’s going to tell you where to GE solver paid the most attention (to the possible detriment of others).

---

<div class="post-metadata">

### Author: ![aledinola](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aledinola](https://discourse.vfitoolkit.com/u/aledinola)
#### Post date: [March 12, 2026, 11:05pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/4 "2026-03-12T23:05:01Z")

</div>

Interesting! Apparently cprintf is a matlab utility written by Yair Altman:

[https://uk.mathworks.com/matlabcentral/fileexchange/24093-cprintf-display-formatted-colored-text-in-command-window](https://uk.mathworks.com/matlabcentral/fileexchange/24093-cprintf-display-formatted-colored-text-in-command-window)

Worth checking compatibility issues with R2025, see discussion in the link above.

---

<div class="post-metadata">

### Author: ![MichaelTiemann](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/michaeltiemann/32/220_2.png) [@MichaelTiemann](https://discourse.vfitoolkit.com/u/MichaelTiemann)
#### Post date: [March 13, 2026, 3:21am UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/5 "2026-03-13T03:21:36Z")

</div>

Since I’m just interested in rendering simple colored text, not HTML or such, no compatibility issues at all. It’s fun watching the red color bounce around some relatively evenly matched GE conditions as the solver does its work.

---

<div class="post-metadata">

### Author: ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)
#### Post date: [March 14, 2026, 9:10pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/6 "2026-03-14T21:10:37Z")

</div>

Apparently the runtime for `cprintf()` is non-negligible (over one second)  
[https://au.mathworks.com/matlabcentral/answers/424450-is-it-possible-to-print-color-text-in-the-command-window](https://au.mathworks.com/matlabcentral/answers/424450-is-it-possible-to-print-color-text-in-the-command-window)

So unfortunately I am going to avoid implementing this. Idea is good, but one second is way to long for something that is non-necessary/convenience and has to be done at every iteration of which there are often hundreds.

PS. On large models you wouldn’t really notice the 1s, but on small models it is going to almost double runtimes.

---

<div class="post-metadata">

### Author: ![MichaelTiemann](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/michaeltiemann/32/220_2.png) [@MichaelTiemann](https://discourse.vfitoolkit.com/u/MichaelTiemann)
#### Post date: [March 15, 2026, 2:12am UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/7 "2026-03-15T02:12:35Z")

</div>

My reading of that post is that it took 1.8 sec to do 100 (1e2) iterations, compared to negligible time to do 100 iterations of fprintf. Would a routine that takes 0.018 extra seconds per iteration score differently in your mind? In the course of printing GE prices, there’s no visible delay as the results are printed out.

---

<div class="post-metadata">

### Author: ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)
#### Post date: [March 15, 2026, 11:39pm UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/8 "2026-03-15T23:39:38Z")

</div>

Fair point, is 0.018s. This is fast enough per stationary general eqm iteration to be irrelevant.

From a practical perspective, looking at the license for [cprintf](https://fd2bcd4f31cdbb6d387a17caaa171bc1a98c8c33) it appears that it can be redistributed as part of VFI Toolkit. It gets updated roughly once a year, so would likely need to be kept up to the latest version (because it is ‘undocumented matlab’ it is the kind of thing liable to break). I guess I could create folder in /Other/cprintf and put both the m-file and the txt license file in there.

In terms of use in toolkit, I would be inclined to have it apply colour only when using heteroagentoptions.verbose=2. Maybe rather than the largest, it just applies to any/all that are more than an order of magnitude larger than the average.

Sound sensible? @MichaelTiemann @aledinola since you two are the ones who want the functionality does this seem like a good approach?

---

<div class="post-metadata">

### Author: ![MichaelTiemann](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/michaeltiemann/32/220_2.png) [@MichaelTiemann](https://discourse.vfitoolkit.com/u/MichaelTiemann)
#### Post date: [March 16, 2026, 1:48am UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/9 "2026-03-16T01:48:05Z")

</div>

As with everything else, users will chime in with their suggestions. Right now I’m using the `err` color (a darker red) to indicate the long pole in the tent and the `comment` color (a sensible green) to show AggVars that are being forwarded from earlier PTypes before the final step. I’m sure others will find cool uses.

Regarding the verbose=2 option, I don’t know how much additional chatter that adds. If the extra chatter makes it difficult to observe the evolution of the GE, then it defeats the purpose that motivated me in the first place.

The toolkit does depend on cost-additional MATLAB add-ons, so I don’t see any downside to relying on a cost-free add-on that users install when they install the toolkit. But I’m new to the MATLAB user community, so don’t count my opinion too much.

---

<div class="post-metadata">

### Author: ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)
#### Post date: [March 16, 2026, 1:52am UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/10 "2026-03-16T01:52:30Z")

</div>

I don’t want ‘dependencies’ beyond the Matlab with Toolboxes, so I would just provide a copy of cprintf() as part of the toolkit, or would not use it at all. [This is what is done with CMA-ES optimization]

---

<div class="post-metadata">

### Author: ![aledinola](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aledinola](https://discourse.vfitoolkit.com/u/aledinola)
#### Post date: [March 16, 2026, 10:08am UTC](https://discourse.vfitoolkit.com/t/cprintf-utility-great-way-to-see-disequilibria/608/11 "2026-03-16T10:08:13Z")

</div>

As far as I understand, cprintf relies on some undocumented matlab features and is likely to break at some point. This requires maintaince. I’m not sure it is worth it.

That said, maybe introducing this only if verbose=2 might be a good compromise.
