Search code examples
stringmatlabtypeconverterfunction-handle

Is there a way to combine multiple functions for plotting without retyping them in MATLAB?


In the classes I'm currently taking, there is a considerable amount of plotting going on. To speed things up, I started using fplot instead of plot. Now I'm wondering if there's a way to combine two functions together without retyping them.

Something like this:

t = @(x) x;
w = @(x) x;

d = @(x) t + w;
fplot(d,[-1,1]);

The ability to do this would allow for much easier debugging and editing, and save a lot of coding time.

One idea I had was using a string to carry the function around.

i.e.

t = 'x';
w = 'x';

d = @(x) t + w;

But I have not been able to find a way of changing it back (except to corresponding ASCII values).

I suppose an alternative would be to use ellipses and break the lines up for easier viewing, but it doesn't solve the core problem.

If you want more background or understanding: Below is an example of some functions we have to plot.

Cn = @(a) (1 - (rn/rc)^2*cos(dc)^2)*cos(dc)^2*sin(2*a);
Ca = @(a) ((1 - sin(dc)^4) * (rn/rc)^2) + (2 * sin(dc)^2 * cos(a)^2 + cos(dc)^2 * sin(a)^2) * (1 - (rn/rc)^2 * cos(dc)^2);

Cl = @(a) ((1 - (rn/rc)^2*cos(dc)^2)*cos(dc)^2*sin(2*a)) * cos(a) - (((1 - sin(dc)^4) * (rn/rc)^2) + (2 * sin(dc)^2 * cos(a)^2 + cos(dc)^2 * sin(a)^2) * (1 - (rn/rc)^2 * cos(dc)^2))*sin(a);
Cd = @(a) ((1 - (rn/rc)^2*cos(dc)^2)*cos(dc)^2*sin(2*a)) * sin(a) + (((1 - sin(dc)^4) * (rn/rc)^2) + (2 * sin(dc)^2 * cos(a)^2 + cos(dc)^2 * sin(a)^2) * (1 - (rn/rc)^2 * cos(dc)^2)) * cos(a);

ld = @(a) (((1 - (rn/rc)^2*cos(dc)^2)*cos(dc)^2*sin(2*a)) * cos(a) - (((1 - sin(dc)^4) * (rn/rc)^2) + (2 * sin(dc)^2 * cos(a)^2 + cos(dc)^2 * sin(a)^2) * (1 - (rn/rc)^2 * cos(dc)^2))*sin(a)) / (((1 - (rn/rc)^2*cos(dc)^2)*cos(dc)^2*sin(2*a)) * sin(a) + (((1 - sin(dc)^4) * (rn/rc)^2) + (2 * sin(dc)^2 * cos(a)^2 + cos(dc)^2 * sin(a)^2) * (1 - (rn/rc)^2 * cos(dc)^2)) * cos(a));

A lot of these are equations we derive ourselves as part of the homework, so if we make a mistake in the derivation of one function, we have to rewrite all of the functions. In this we were given Cn and Cl (which used Cn and Ca), but have to derive Ca and Cd ourselves. "ld" is simply cl divided by cd.

Simplified, they might look like this:

Cl = @(a) Cn * cos(a) - Ca * sin(a)
Cd = @(a) Cn * sin(a) + Ca * cos(a)

ld = @(a) Cl / Cd

I've gotten a lot of help perusing this site for answers, but I have been unable to find anybody else asking this question or a similar one. If you know of another post that has a solution for this, I would be quite pleased to read that first. Hope you can help!


Solution

  • Even if I'll get the nickname "Captain Obvious", I'm going to state the straightforward way of composing function handles. :-)

    %'The operators .*, .^ and ./ are safer when "a" is not scalar'
    %'assuming that everything else is'
    Cn = @(a) (1 - (rn/rc)^2*cos(dc)^2)*cos(dc)^2*sin(2*a);
    Ca = @(a) ((1 - sin(dc)^4) * (rn/rc)^2) + (2 * sin(dc)^2 * cos(a).^2 + cos(dc)^2 * sin(a).^2) * (1 - (rn/rc)^2 * cos(dc)^2);
    
    Cl = @(a) Cn(a) .* cos(a) - Ca(a) .* sin(a);
    Cd = @(a) Cn(a) .* sin(a) + Ca(a) .* cos(a);
    
    ld = @(a) Cl(a) ./ Cd(a);
    

    The call is obvious again:

    ld(3);
    ld([1,2;3,4]);