How to generate a 2 dimensional array when using a subs function with symbollic function array

Good day,
I will try to explain what i want to do as plainly as possible.
I have two vectors:
  • One vector contains 4 symbollic functions which are a result of linsolve matrix calculation. All functions contain only one variable "omega".
  • Below that i have created a second vector of 2001 variables containing a range of "omega" [0:0.01:20].
Now, what i wanted to do is create a [2 2001] matrix containing calculated values for two functions created on the basis of the previous symbollic vector, while also substituting the omega in those symbollic functions in the same step. What i'm certainly doing wrong is that i'm wrongly defining the matrix (i'm getting the array size error). Could you help me with syntax (or probably suggest a more reasonable approach) as i'm having a trouble wrapping my head around this issue by relying only on syntax provided with help pages.
I don't want to define functions directly (like A1, A2 etc.) because i want the code to work for every possible number of defined amplitudes.
Posting a relevant snippet of code below:
%Matrix solver
[C,F] = equationsToMatrix(Eqn,vars)
%V = vars.'
V = linsolve(C,F) %We are getting a vector of 4 symbollic functions as a result (just specific to this case - could as well be 2 or 6 or more)
syms A
omega = 0:0.01:20; %Omega variable vector definition
%Amplitude calculator
for i=1:length(M) %M is another vector containing masses (its length will always be like "V" divided by 2)
A(i,:) = sym(subs(sqrt(V(2*i-1)^2+V(2*i)^2))); %This is where i'm getting an error, i don't know how to define "A" to properly iterate through both "omega" and "i"
end

1 comentario

In the meantime, i have coded in a very crude workaround that works, but does not use the full extent of "subs" function possibilities. This code is therefore very slow and i think it could be optimised much better but i can't think of a good solution.
%Amplitude calculator
for i=1:length(M)
A(i) = sym(sqrt(V(2*i-1)^2+V(2*i)^2));
end
for i=1:length(M)
for j=1:2001
A(i,j) = subs(A(i),omega,j/100);
end
end

Iniciar sesión para comentar.

Respuestas (1)

I think i have gradually learned how to do it semi properly. At least i managed to achieve much greater performance than my previously proposed "stiched togather" solution. This is what i have come up with.
I have preallocated memory for another matrix, in which i'm storing the calculated values of substituted formulas. I previously didn't know how to properly formulate the syntax to do such operation. In case someone stumbles upon the same problem, i'm posting my "better" solution below. I would also much appreciate a review of this code by more experienced members of this community and some hints as to how could i optimise it further.
%Matrix solver
[C,F] = equationsToMatrix(Eqn,vars)
V = vars.'
V = linsolve(C,F)
syms A Ph
omega = 0:0.01:20;
%Amplitude calculator
for i=1:length(M)
A(i) = sym(sqrt(V(2*i-1)^2+V(2*i)^2));
end
sA = zeros(length(M),length(omega));
for i=1:length(M)
sA(i,:) = subs(A(i));
end

Categorías

Productos

Versión

R2021b

Preguntada:

el 15 de Mzo. de 2023

Respondida:

el 20 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by