필터 지우기
필터 지우기

Subs does not work in my code.

조회 수: 3 (최근 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

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by