Concerned that matlabFunction( ), when converting from symbolic to numerical, changes the sizes of matrices and vectors,

Hi there!
I am currently concerned that the function matlabFunction( ), when converting from symbolic to numerical, changes the sizes of matrices and vectors. For instance, I started with a 3x3 symbolic matrix M, gotten from equationsToMatrix( ). Then, I simplified this matrix using simplify( ). And, finally, I used matlabFunction to convert from symbolic to numerical, and to write the numerical matrix to a separate file. Now, when I go to check this separate file that's generated by Matlab, here is the last line, which gives the matrix M, and, it doesn't look 3x3 anymore. Does matlabFunction change the size of symbolic matrices and vectors, when converting from symbolic to numerical? If so, how can I preserve the original sizes of the symbolic matrices and vectors, after the conversion to numerical? Please see M below, generated by Matlab; it looks maybe 2x2 now, but I can't be sure. I don't know the 'reshape' command that Matlab used. But, it kind of looks 2x2, and, perhaps certainly, no longer 3x3. That is weird.
Another example is that my 3x1 vector 'b' no longer looks 3x1; it now looks 2x1, generated by Matlab.
Any help is greatly appreciated; thanks in advance!
mt1 = [(af.*m_perp_added.*(t26.*sin(t45)-1.0))./2.0-m_parallel_added.*t3.*t18.*t48+FD.*t2.*t18.*t47.*t53+FL.*t2.*t18.*t50.*t53;g.*(m_D+m_wing)-(af.*m_perp_added.*(t26.*cos(t45)+1.0))./2.0-V.*g.*rho_f-m_parallel_added.*t4.*t18.*t48+FD.*t2.*t18.*t50.*t53-FL.*t2.*t18.*t47.*t53];
mt2 = [(A.*omega.*rho_f.*w.^3.*abs(omega))./1.28e+2-t2.*t18.*t53.*(-yG+t18.*(m_D.*yG+m_wing.*yG-d_CD.*m_D.*t4)+(t2.*t18.*t56.*w.*(t6+t8+t25+t5.*t15-t6.*t14+t7.*t15-t8.*t14+t9.*t14+t15.*t25))./8.0).*(-t16+t31+FD.*t5+FD.*t7+FD.*t30+FL.*t6+FL.*t8+FL.*t40)-t2.*t18.*t53.*(-xG+t18.*(m_D.*xG+m_wing.*xG-d_CD.*m_D.*t3)+(t2.*t3.*t18.*t56.*w.*(t21+t22+t23+t24+t3.*t25+t4.*t25))./4.0).*(t16+t31-FD.*t6-FD.*t8+FD.*t29+FL.*t5+FL.*t7+FL.*t30)-V.*g.*m_D.*rho_f.*t18.*(xG-d_CD.*t3)];
b = [mt1;mt2];
M = reshape([-t4.*t12.*(m_parallel_added+m_wing+t13+t8.*t11),t22,0.0,t22,-t4.*t12.*(m_perp_added+m_wing-t13+t14),0.0,m_D.*m_perp_added.*t12.*t17,m_D.*m_perp_added.*t9.*t12,-m_D.*(t20+t21)-t12.*(t5.*t20+t5.*t21)-(m_wing.*(h.^2+w.^2))./1.2e+1],[3,3]);

 Risposta accettata

As you can see
M = reshape([...],[3 3])
Thus the expression in [ ] is 1x9, but it is finally reshaped to 3x3.

13 Commenti

Hi Torsten!
I also posted my vector 'b'; it was originally 3x1, symbolically, but now it looks 2x1, after conversion to numerical. What do you think? Thanks!
mt1 is 2x1, mt2 is 1x1, thus b is 3x1. Search for the ";" in mt1 that separates the two entries.
Yes, got it! That was hard to see! :)
I am getting a matrix dimension error, when I try to calculate M \ b.
But my numerical matrix M is 3x3, and my numerical vector b is 3x1.
So, the matrix solve M \ b should work, yet it does not.
Hmm ...
Then my guess is that not all variables that make up M and b are scalar values.
Why don't you follow my advice and insert the commands
size(M)
size(b)
just before you try M\b ???
Interesting, Torsten!
The matrix M is 3x3, but the vector b is ... 9x1!
How did that happen?
We just saw that the vector b was 3x1.
We just saw that the vector b was 3x1.
No, we saw that the vector b is made up of three parts. Depending on the variables that make up b, all three parts could be 3x1, e.g., which would give a total of 9x1 for b.
So apply the same "size" command to the three parts that make up b to see what's going on.
Ok! Will do this after dinner —- thanks so much!
Hi Torsten!
I applied the size( ) command to the symbolic matrix M and vector b and see that M is 3x3 and b is 3x1, as desired. So, could it be that matlabFunction( ), when converting from symbolic to numerical, can change the sizes of matrices and vectors?
Hi Torsten,
If you're curious, the issue was that I was writing some symbolic vectors as linear combinations of basis vectors, while writing other vectors abstractly, without expressing them in terms of basis vectors. This inconsistency led to some variables coming out too big, namely, 3x1. I've now written all of my symbolic vectors as linear combinations of basis vectors, and the issues appear to be fixed, and the numerical matrix M and numerical vector b appear to have the correct sizes. Just want to say thanks again for your help!
Goodnight!
Glad that I could help. Good luck for your further work.

Accedi per commentare.

Più risposte (1)

The expression is creating a 1 x 9 vector and reshape() it to 3 x 3. It may be a bit clumsy compared to directly generating a 3 x 3, but it is workable code.

3 Commenti

I see; thanks so much for your quick response, Walter! Let me keep debugging then.
Hi Walter,
What about my 3x1 vector 'b' that I just added to this question? It no longer looks 3x1; it looks 2x1 to me. Thanks!
Ok, got it Torsten! Then my bugs are elsewhere then. I should keep debugging.
Thanks again!

Accedi per commentare.

Prodotti

Release

R2024b

Richiesto:

il 26 Nov 2024

Commentato:

il 27 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by