bio_img_community

MATLAB Community

MATLAB, community & more

Making Live Scripts with Generative AI

I've been having fun making MATLAB code with Large Language Models. You can try this yourself with MATLAB's Copilot feature. But since the AI world is moving so fast, I also like to do a little generative AI tourism, trying out the latest models and tools. Today I want to look at Anthropic's Claude Code.

Claude Code runs in a terminal window that can sit inside an IDE like Microsoft's VS Code. You do need a payment plan with Anthropic in order to make it work. So be aware that the demos I'm stepping through here involved me spending a little money. The total cost of the experiments I describe here was $0.63 using Claude's pay-as-you-go option.

What to code? Since we're just playing around, let's take a visually appealing topic: fractal attractor systems. I started off with a vague prompt, curious to see what it would come up with.

> Make me a MATLAB Live Script demonstrating the De Jong fractal attractor

The De Jong attractor is a two-dimensional system defined by these equations.

xt+1 = sin(a · yt) - cos(b · xt)
yt+1 = sin(c · xt) - cos(d · yt)

Claude gets right back to me with a result, DeJongFractalAttractor.mlx, but something is wrong.

Claude (I'm using the Sonnet 4 model here) knows about MATLAB's Live Scripts. And it knows that Live Scripts use the .MLX suffix. But it doesn't know how to create the required OPC format. Instead it writes out a plain text MATLAB script and gives it the .MLX suffix. The resulting file won't even open in MATLAB. If I change the suffix to .M, the file is revealed to be an old-school plain text MATLAB script, complete with %% section breaks.

I can use the PUBLISH command to turn this file into HTML, but we can do better now! One of my favorite recent additions to MATLAB is the plain text Live Script. With this format I get the best of all worlds. All the code, rich text, and images are in a single notebook file, so I don't have a dead HTML file with numerous nearby images hiding in a subfolder. Furthermore, that single file is a plain text .M file! So I don't have the hassle of a complex .MLX format that GitHub can't understand.

So after I change the suffix to .M and then open it as a Live Script and then save it as a MATLAB Live Code File .M file, I end up with something that looks pretty good. But that's a lot of steps to go through just to get to a decent output!

So this generative AI thing is kind of a bust, eh? After all, it fumbled my request. But wait! Here is the main point I want to make today: with some extra prompting, you can often teach the AI how you want it to behave. Out of the box, Claude doesn't know about plain text Live Script files. But I can teach it. I put some coaching prompts in a markdown file called Live_Script_rules.md. Here are some of the contents of that file.

## Live Scripts
- If creating a script, use the plain text Live Script format with the .m suffix
- DO NOT create .mlx scripts
- DO NOT create .m scripts that are not Live Scripts
- When creating new plain text Live Scripts, close with an appendix:
    %[appendix]{"version":"1.0"}
    %---
    %[metadata:view]
    %   data: {"layout":"inline"}
    %---
- Section heads should look like this:
    %%
    %[text] ## Section Title
- DO NOT do this: `%% Section Title`
- Normal text intended for a single paragraph should appear on a single line
- Equations in Live Scripts are formatted using LaTeX formtting like this.
    %[text] $ e = \\sum\_{\\alpha=0}^\\infty \\alpha^n/n! $
- Note the double backslashes
- Use implicit figure creation
- DO NOT use the "figure" command to create new figures
- DO NOT start scripts with CLOSE and CLEAR commands
- Bulleted lists in a rich text block must close with a backslash. Here is an example.
    %[text] - bullet 1
    %[text] - bullet 2
    %[text] - bullet 3 (note the trailing backslash for the last bullet) \

I can go back to Claude and say

> Please regenerate that file, but using these rules: @Live_Script_rules.md

Now the situation is much improved. As long as I keep this rules file in context, I can create beautiful new plain text Live Script files all day long. Here's one last prompt to show what I mean.

> Use the file DeJongFractalAttractor.m as a template and generate two more similar files for these attractors: Dadras attractor and Aizawa attractor

Since my rules were still in context, I was immediately rewarded with two more Live Scripts that met all my standards, no post-editing required.

I have found it very useful to remind myself that, whenever I'm not happy with the AI model's generated output, there is often a prompt that can rescue the situation. Once you find that extra prompt, you can make it a part of your practice, and the world gets better very quickly. So consider hesitating a bit before you declare a model to be stupid. Before you've made an effort to help it out. You might be able to salvage the situation and emerge victorious.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.