MATLAB Incorrect number or types of inputs or outputs for function solve.

123 ビュー (過去 30 日間)
Aryan
Aryan 2025 年 2 月 13 日
回答済み: Steven Lord 2025 年 2 月 13 日

Hi, I am trying to solve for p here This code gives error Incorrect number or types of inputs or outputs for function solve.

Error in stats1 (line 16) solutions = solve(sf == 0, p);

if true
  % clc
syms p 
n = input('Enter how many times to repeat vars in combinations(): ');
R = repmat({[p, p-1]}, 1, n);  
T = combinations(R{:});
disp(['Combinations of vars repeated ', num2str(n), ' times:']);
disp(T);
pm = log(prod(T, 2));
ps = prod(T, 2);
k = ps .*pm ;
disp('Product of each combination:');
disp(k)
sm = sum(k);
disp(sm)
sf = diff(sm, p)
solutions = solve(sf == 0, p);
end

回答 (1 件)

Steven Lord
Steven Lord 2025 年 2 月 13 日
Let's choose a small value for n (since the input command cannot run on MATLAB Answers) and see what you're trying to pass into the solve function.
n = 3;
syms p
R = repmat({[p, p-1]}, 1, n);
T = combinations(R{:});
disp(['Combinations of vars repeated ', num2str(n), ' times:']);
Combinations of vars repeated 3 times:
disp(T);
Var1 Var2 Var3 _____ _____ _____ p p p p p p - 1 p p - 1 p p p - 1 p - 1 p - 1 p p p - 1 p p - 1 p - 1 p - 1 p p - 1 p - 1 p - 1
pm = log(prod(T, 2));
ps = prod(T, 2);
k = ps .*pm ;
disp('Product of each combination:');
Product of each combination:
disp(k)
prod ____________________________ p^3*log(p^3) p^2*log(p^2*(p - 1))*(p - 1) p^2*log(p^2*(p - 1))*(p - 1) p*log(p*(p - 1)^2)*(p - 1)^2 p^2*log(p^2*(p - 1))*(p - 1) p*log(p*(p - 1)^2)*(p - 1)^2 p*log(p*(p - 1)^2)*(p - 1)^2 log((p - 1)^3)*(p - 1)^3
sm = sum(k);
disp(sm)
prod __________________________________________________ log((p - 1)^3)*(p - 1)^3 + p^3*log(p^3) + 3*p*l...
sf = diff(sm, p)
sf = table
prod __________________________________________________ 3*log((p - 1)^3)*(p - 1)^2 + 6*(p - 1)^2 + 3*p^...
The solve function is not designed to accept a table as the first input. But if you extract the value from the table and use that in your solve call:
sol = solve(sf.prod == 0, p)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
sol = 
Or if you extracted the matrix of symbolic variables from the table before you started operating on it:
T2 = T.Variables
T2 = 
pm = log(prod(T2, 2));
ps = prod(T2, 2);
k = ps .*pm ;
sm = sum(k);
sf2 = diff(sm, p);
sol2 = solve(sf2 == 0, p)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
sol2 = 
isequal(sf2, sf.prod)
ans = logical
1

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by