Discrepancy between normal computations and the "subs" function results.

I'm dealing with quadratic equations, so i have something like:
k1 * x1^2 + k2 * x2^2 (quadratic terms) + k3 * x1 * x2 (mixed term) + k4 * x1 + k5 * x2 (linear terms) + k6 (constant)
Note: the equations im dealing with are way more complicated, they have more quadratic, mixed and linear terms. Im just simplifying the equation to explain the problem.
I know both the coefficients (k1, k2, ..., k6) and the unknown parameters (x1, x2). So i just have to substitue these values in the equation and get the result.
The problem is this:
If i use the "subs" function (result = subs(equation, x, unknown_parameters_values) i get one result. Note that "equation" is a symbolic function of course.
If i compute the products and sum them i get another result, which differs a lot from the one i get from using the "subs" function.
I know it is an apporximation problem, but is there a way I can fix it? Also what result is more accurate, the first one where i use the "subs" function or the one where i compute the terms one by one? I would like to get at least 2 similar results.
Thank you for your help.

5 Kommentare

Just to be clear, is "compute the products and sum them" all being done using Symbolic sym objects?
For example, suppose we have
syms k1 x1 x2
equation = k1*x1^2 + k3*x1*x2
Can you show how you're executing the subs command and how you would compute the products and sum them for representative values of k1, k3, x1, and x2.
Sorry I forgot to specify that. I have the coefficents k and the unknown_values stored in 2 arrays of double.
So i have something like:
k = [1 2 3 4 5 6];
uv = [10 20];
result = k[1]*uv[1]*uv[1]+k[2]*u[2]*u[2]+...
The result i get this way is very different from the result i get this way:
syms x[2 1];
equation = k1*x1^2 + ...
result = subs(equation, x, uv);
In the symbolic equation are included the coefficients "k". The only symbolic terms are x1 and x2.
It would be very helpful to provide a simple example of working code, otherwise we are just guessing. Is this what the workflow looks like?
k = [1 2];
syms x [2 1];
equation = k(1)*x(1)^2 + k(2)*x(2)^2;
uv = [10; 20];
y1 = subs(equation,x,uv)
y1 = 
900
y2 = k(1)*uv(1)*uv(1) + k(2)*uv(2)*uv(2)
y2 = 900
Matt J
Matt J am 13 Sep. 2023
Bearbeitet: Matt J am 13 Sep. 2023
which differs a lot from the one i get from using the "subs" function.
Please also elaborate on "differs a lot". What is the relative error?
@MDL Please attach the code you are working with or relevenat portion of it.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Gefragt:

MDL
am 13 Sep. 2023

Kommentiert:

am 13 Sep. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by