Search code examples
arraysmatlabdimension

Subscripted assignment dimension mismatch in for loop with solve function outputting 2x1 from 1x1 inputs


In MATLAB, I am trying to run an iterating solve function that solves a quadratic equation. Each iteration enters the loop as a 1x1 matrix from a 1x6 array, but the for loop wants to shoehorn the 2x1 answer into the 1x1 allocated space.

I've researched cells and structures but to no avail could I get them to work. The equation works if take the for loop off and just solve for each function individually, but the goal is to scale this for loop up to churn through a lot more than just a 1x6 array. Here is my code.

    Es=200E3;        %MPa
    Ys=448;          %MPa
    D=168.3;         %mm
    wall=7.11;       %mm
    Pdesign=27.25;
    Ec=23800;
    strainc=.003;
    ts=7.11-7.11*.8;

    Plive=[5.45 8.18 10.90 13.63 16.35 19.08];
    syms trepair;

    for ii=1:1:length(Plive)

        test(ii)=solve(strainc==(Pdesign*D)/(2*Ec*trepair)-Ys*ts/(Ec*trepair)-Plive(ii)*D/(2*(Ec*trepair+Es*ts))); 

    end

Thanks in advance for y'alls input.


Solution

  • The results are 2x1 arrays, so you need to assign them to 2x1 arrays, not 1x1 arrays. Replace test(ii) with test(2*ii-1:(2*ii)).

    Alternatively, solve with PLive as a symbolic variable, and then use matlabFunction to get solutions for whatever values you'd like:

    Plive=[5.45 8.18 10.90 13.63 16.35 19.08];
    syms trepair Pl
    T=matlabFunction(solve(strainc==(Pdesign*D)/(2*Ec*trepair)-Ys*ts/(Ec*trepair)-Pl*D/(2*(Ec*trepair+Es*ts)),trepair));    
    T(Plive)