matlabmatlab-app-designer

e to the power of a polynomial throws the error: "Error using ^ (line 52) Incorrect dimensions for raising a matrix to a power"


I am currently making an app for a class that calculates the time it takes for an abject to reach terminal velocity. The function that I've found, requires for there to be e to the power of a certain polynomial. When I set my variable e to exp(1) MATLAB throws an error that has been outlined in the title.

To solve this issue, I've tried using euler(1) instead of exp(1), but this didn't seem to help. I have no idea why MATLAB is saying that exp(1) is a matrix, rather than a scalar. My code is bellow:

e = euler(1);

app.MaxMassEditField.Value = 100;
app.AreaEditField.Value = 1;
app.DragCoefficientEditField.Value = 1;
app.gEditField.Value = 1;
app.MediumDensitryEditField.Value = 1;
app.MassSlider.Limits = [10^(-7), app.MaxMassEditField.Value];

time = linspace(0, 10, 1000);
velocity = - (1-e^(2*sqrt((app.gEditField.Value*app.AreaEditField.Value*app.MediumDensitryEditField.Value*app.DragCoefficientEditField.Value)/(2*app.MassSlider.Value))*time))/(1+e^(2*sqrt((app.gEditField.Value*app.AreaEditField.Value*app.MediumDensitryEditField.Value*app.DragCoefficientEditField.Value)/(2*app.MassSlider.Value))*time));

plot(app.UIAxes, time, velocity)

Solution

  • Firstly, you need to remember that e^1 is 0, not e. So exp(1) and euler(1) won't work. In Matlab you don't have to calculate e separately. Instead, you can use the exp() function to do the exponentiation. So, exp() should go in your mega-equation. Doing that gets rid of the error message you are seeing, but you still don't get a sensible result.

    Next, your equation is absolutely huge. Things are a lot easier to debug if you break it down into manageable chunks and give your variables shorter names. Doing that, I quickly spot that you probably need to do an element-wise division, and I think it now works.

    Code:

    A = 1;
    Drag = 1;
    g = 1;
    Density = 1;
    Mass = 1;
    time = linspace(0, 10, 1000);
    
    exponent = exp(2*sqrt((g*A*Density*Drag)/(2*Mass))*time);
    velocity = - (1-exponent)./(1+exponent);
    
    plot(time, velocity)
    

    Plot: VelocityTimePlot