How can I make a loop for solving a quadratic equation with 200 different data points and only include the positive values of the quadratic eqaution?
3 views (last 30 days)
Show older comments
p1 = RelaxKineticsData.pH0_2
p1e = 6.7240 p1f = p1 - p1e c1H = 10.^(-p1f) t
1 = RelaxKineticsData.Time0_2
Ka = 0.00000074 Kd = 50
Cr1 = 0.086
syms x eqn = (Ka*x)/c1H + x + Kd*x^2 - 0.086 == 0 double(solve(eqn,x)) So far this is what I have.
I also tried p1 = RelaxKineticsData.pH0_2;
p1e = 6.7240;
p1f = p1 - p1e;
c1H_values = 10.^(-p1f);
t1_values = RelaxKineticsData.Time0_2;
Ka = 0.00000074;
Kd = 50;
Cr1 = 0.086;
x_values = zeros(size(c1H_values));
for i = 1:length(c1H_values)
c1H = c1H_values(i);
eqn = (Ka*x)/c1H + x + Kd*x^2 - Cr1 == 0;
x_values(i) = double(solve(eqn, x));
end
and got this error : Unable to perform assignment because the left and right sides have a different number of elements. Error in untitled2 (line 20) x_values(i) = double(solve(eqn, x));
This seems like a rather simple task, just calculating positive values of a quadratic equation for a set of data, but It's been giving me alot of trouble.
0 Comments
Answers (1)
Dyuman Joshi
on 21 Apr 2023
Edited: Dyuman Joshi
on 21 Apr 2023
"eqn" is a quadratic equation in x and thus using solve() will give you 2 outputs. But you are trying to assign 2 outputs to one numeric place holder, which gives you the error.
n = numel(c1H_values);
%Initiate x_values as a two column array to
%store both roots of the equation
x_values = zeros(n,2);
for i = 1:n
c1H = c1H_values(i);
eqn = (Ka*x)/c1H + x + Kd*x^2 - Cr1 == 0;
x_values(i,:) = double(solve(eqn, x));
end
You can set non-positive roots of the equations to 0 or NaN.
In case you just want to obtain the positive roots, define x_values as a cell array and proceed further.
4 Comments
Dyuman Joshi
on 22 Apr 2023
Like you did earlier -
p1 = RelaxKineticsData.pH0_2;
p1e = 6.7240;
p1f = p1 - p1e;
c1H_values = 10.^(-p1f);
I only showed a part of your code.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!