필터 지우기
필터 지우기

Tough situation in trying to combine matrix indexing, symsum, and outputting the answer as a symbolic sum to be plotted.

조회 수: 1 (최근 30일)
How would I go about automating this while still maintaining a symbollic output?
M_cvec = magic(9)
psi_0 = 2*pi^(-1/2);
Psi_cx1 = psi_0...
+ M_cvec(1,1)*pi^(-1/2)*cos(2*pi*x/a)...
+ M_cvec(2,1)*pi^(-1/2)*cos(2*2*pi*x/a)...
+ M_cvec(3,1)*pi^(-1/2)*cos(3*2*pi*x/a)...
+ M_cvec(4,1)*pi^(-1/2)*cos(4*2*pi*x/a)...
+ M_cvec(5,1)*pi^(-1/2)*cos(5*2*pi*x/a)...
+ M_cvec(6,1)*pi^(-1/2)*cos(6*2*pi*x/a)...
+ M_cvec(7,1)*pi^(-1/2)*cos(7*2*pi*x/a)...
+ M_cvec(8,1)*pi^(-1/2)*cos(8*2*pi*x/a)...
+ M_cvec(9,1)*pi^(-1/2)*cos(9*2*pi*x/a);
The code needs to be such that the output is of the form
Psi_cx(m) = psi_0 + M_cvec(n,m)*cos(n*(2*pi*x/a))*pi^(-1/2)
But a for loop can't handle x as a symbol, while symsum can't handle the matrix indexing.
The final equation will be plotted along with many more sum series of the same form

답변 (1개)

Vishesh
Vishesh 2023년 9월 18일
I understand that you want to write a script that automate the formula given below:
Psi_cx(m) = psi_0 + M_cvec(n,m)*cos(n*(2*pi*x/a))*pi^(-1/2)
You can do it using following script:
M_cvec = magic(9);
psi_0 = 2*pi^(-1/2);
%Create symbolic scalar variables
syms x;
syms a;
%lets m=1 and n=9
m=1;
n=9;
%storing result of each 'm' values.
Psi_cx=[];
for i=1:m
Psi_cxTemp=psi_0;
for j=1:n
Psi_cxTemp=Psi_cxTemp+M_cvec(j,i)*cos(j*(2*pi*x/a ))*pi^(-1/2);
end
Psi_cx=[Psi_cx Psi_cxTemp];
end
%Now use "subs" for substituting symbolic scalar variables with their respective values.
% Example : for x=1,m=1 and a=1
Psicx1=subs(Psi_cx(1),x,1);
Psicx1=subs(Psicx1,a,1);
Here, we are using "x" and "a" as a symbolic scalar variable so that we can substitute it with their respective values later using "subs" function.
Please refer to the following documentation for more information on "Symbolic scalar variables" and "Substituting symbolic scalar variables":
  1. For "Symbolic scalar variables": Create symbolic scalar variables and functions, and matrix variables and functions - MATLAB syms - MathWorks India
  2. For "Substituting symbolic scalar variables": Symbolic substitution - MATLAB subs - MathWorks India

카테고리

Help CenterFile Exchange에서 Get Started with Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by