Subs does not work in my code.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
Dyuman Joshi
il 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
1 Commento
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!