How to solve quadratic equation which includes a vector?

Hi,
I have the following equation: V1^2 - V1*V2 - Q*Z = 0
V1 is what I need to find
V2 is a known constant
Q is a known vector with complex values
Z is a known constant
This is what I've tried:
syms V1
eq = V1^2 - V1*V2 - Q*Z;
result = roots(eq);
But I get the result "Empty sym: 0-by-1"

 Accepted Answer

You can try with solve:
syms V1
eq = V1^2 - V1*V2 - Q*Z;
result = solve(eq,V1);

6 Comments

I tried and get a result that states: val = x.
Is there a way to solve it with a for-loop perhaps?
You can try with eval():
syms V1
eq = V1^2 - V1*V2 - Q*Z;
result = solve(eq,V1);
result = eval(result)
or roots()
V2 = 1; Q = 2; Z = 3; %Values of example
eq2 = [1 -V2 -Q*Z]; %Equivalent to V1^2-V1*V2-Q*Z
result2 = roots(eq2);
to this case don't need for loop
when using eval() i get a 0x0 double (no result)
whereas when trying to use roots() I get the error: "Dimensions of arrays being concatenated are not consistent"
I think I need to try a for loop like:
for i = 1:length(Q)
syms x
eqn = x^2 - V2*x - Q*Z;
solx = solve(eqn, x);
end
But it doesn't work either, as I get: "Empty sym: 0-by-1"
Now understand, i don't considered Q with a vector. In this case you need to use a loop.
Q = [1+3i 2+2i 3+4i 4+2i]; %Example
V2 = 1; Z = 8;
for i = 1:length(Q)
eq2 = [1 -V2 -Q(i)*Z];
result2 = roots(eq2)
result{i} = result2;
end
sorry :D
In this case you should only change Q by Q (i)
for i = 1:length(Q)
syms x
eqn = x^2 - V2*x - Q(i)*Z; %% Change Q ---> Q(i);
solx = solve(eqn, x);
end
The former loop worked, yet I only get one result: the positive and negative result of the last value of my Q-vector... Shouldn't I get a vector/matrix as a result? Maybe roots isn't the right function for this case...? For some reason it's not saving all the values yet just the last one
Figured it out; it's
for i = 1:length(Q)
eq2 = [1 -V2 -Q(i)*Z];
result(i,:) = roots(eq2)
end
Thank you!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!