Subs does not work in my code.

2 ビュー (過去 30 日間)
Valeria Ramirez Ariiola
Valeria Ramirez Ariiola 2024 年 4 月 29 日
コメント済み: Dyuman Joshi 2024 年 5 月 28 日
Hi, my function should replace all the monomials that have negative degree from -M to -(4*M+8) with 1, since 0 will give an error, nevertheless, it doesn't work the substitution function. Every time I try it, it doesn't work. Could you please give me some ideas on how to solve this issue.
function [sMnew] = Cubic(k,M)
n= 3*k+1;
x= sym('x');
c = sym( 'c', [1 M+3]);
% Define the sum of monomials with negative coefficients
CC = x;
for i = 2:M+3
CC = CC + c(1,i)*x^(-i+1);
end
sM = expand(CC^(n));
for i = M:1:4*M+8
sMnew = subs(sM, x^(-i), 1);
end

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2024 年 4 月 29 日
You can vectorize operations in the code -
function [sMnew] = Cubic(k,M)
n = 3*k+1;
x = sym('x');
c = sym( 'c', [1 M+3]);
%Indices
k = 2:M+3;
%Vectorized sum
CC = x + sum(c(1,k).*x.^(-k+1));
%Define powers for which to substitute
vec = -1*(M:4*M+8);
%Substitute directly at once
sMnew = subs(z, x.^(vec), ones(size(vec)));
end
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2024 年 5 月 28 日

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by