Subs does not work in my code.

2 views (last 30 days)
Valeria Ramirez Ariiola
Valeria Ramirez Ariiola on 29 Apr 2024
Commented: Dyuman Joshi on 28 May 2024
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

Answers (1)

Dyuman Joshi
Dyuman Joshi on 29 Apr 2024
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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!