Subs does not work in my code.

2 visualizaciones (últimos 30 días)
Valeria Ramirez Ariiola
Valeria Ramirez Ariiola el 29 de Abr. de 2024
Comentada: Dyuman Joshi el 28 de Mayo de 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

Respuestas (1)

Dyuman Joshi
Dyuman Joshi el 29 de Abr. de 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

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by