How to Substitute an array into an equation and solve it ?

조회 수: 8 (최근 30일)
Kalpha.mc
Kalpha.mc 2020년 11월 15일
편집: Ameer Hamza 2020년 11월 16일
So if i had a = [1 2 3] b = [4 5 6] c = [7 8 9]
and the equation is ax^2+bx+c = 0
How would i substitude the array and solve for x ?

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 15일
편집: Ameer Hamza 2020년 11월 15일
For the given value of a, b, and c, the equation have no real solutions. Following shows how to use fsolve() when the equations have solutions
a = [1 2 3];
b = [4 5 6];
c = [-7 -8 -9];
F = @(x) a(:).*x.^2+b(:).*x+c(:);
sol = fsolve(F, rand(3,1))
If you don't have optimization toolbox, then you can use fzero()
a = [1 2 3];
b = [4 5 6];
c = [-7 -8 -9];
sol = zeros(size(a));
for i=1:numel(a)
F = @(x) a(i).*x.^2+b(i).*x+c(i);
sol(i) = fzero(F, rand());
end
  댓글 수: 2
Ameer Hamza
Ameer Hamza 2020년 11월 15일
No, you are not using subs() correct. Following shows how it needs to be done
syms x a b c
C = a * (x.^2) + (b * x) + c == 0;
x1 = subs(C,{a,b,c},{1,3,5});
solution = double(solve(x1,x));
Ameer Hamza
Ameer Hamza 2020년 11월 16일
편집: Ameer Hamza 2020년 11월 16일
If a, b, and c are arrays
syms x a b c
av = [1 2 3];
bv = [4 5 6];
cv = [7 8 9];
C = a * (x.^2) + (b * x) + c == 0;
solution = cell(size(av));
for i = 1:numel(av)
x1 = subs(C,{a,b,c},{av(i),bv(i),cv(i)});
solution{i} = double(solve(x1,x));
end
solution is a cell array, where each cell contain solution for a combination of a, b, and c.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by