Changing the name of a variable changes the results?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
David
el 12 de Dic. de 2023
Comentada: David
el 12 de Dic. de 2023
I have very simple script that I am using to solve a set of simultaneous equations. Note the variable named "RB" (it stands for reaction force at point B). In it's current state the code geneates an erroneous solution for RB. The value should be 2.3467e+03 (or the negative of this value). However it reports 1.6762e+03, which, by the way, should be the solution for the variable "L". It's effectively flipping the results for variables "RB" and "L".
To address this issue, I simply have to change variable "RB" to "B" and it gives the correct results. Why does simply changing the name of a variable change the output? Does placing an "R" in front of a variable somehow reverse its value? It is recognizing "RB" as a variable.
Your insights appreciated!
% Clear Commands
clc; clear all;close all;
% define symbolic variables
syms RB L theta;
%this makes output real numbers
sympref('floatingpointoutput',true);
% here are the equations from the hand set up
e1=RB*cosd(30)+3500*cosd(theta);
e2=-L+3500*sind(theta)-RB*sind(30);
e3=(0.35*L)-(0.25*RB);
% solve for the unknowns (all in kN)
[RB, L, theta]=solve(e1,e2,e3);
RB
L
theta
2 comentarios
Rik
el 12 de Dic. de 2023
I suspect this has to do with the variable names, considering RB is after L in the alphabet and B is before L in the alphabet. This might affect choices the symbolic engine makes.
I must admit I don't see why that would happen.
Respuesta aceptada
Dyuman Joshi
el 12 de Dic. de 2023
Editada: Dyuman Joshi
el 12 de Dic. de 2023
When the variables to solve for are not provided in the call to solve(), MATLAB calls symvar on the equations, which outputs the variables in alphabetical order with uppercase preceding lower case.
From the documentation of solve, vars tab - "Variables for which you solve an equation or system of equations, specified as a symbolic vector or symbolic matrix. By default, solve uses the variables determined by symvar.
The order in which you specify these variables defines the order in which the solver returns the solutions."
Imo, it is best to specify the order in which the solution is expected.
% Clear Commands
clc; clear all;close all;
% define symbolic variables
syms RB L theta;
%this makes output real numbers
sympref('floatingpointoutput',true);
% here are the equations from the hand set up
e1=RB*cosd(30)+3500*cosd(theta);
e2=-L+3500*sind(theta)-RB*sind(30);
e3=(0.35*L)-(0.25*RB);
eqn = [e1 e2 e3]
%symvar on the equations
symvar(eqn)
%Output when variables are not specified
sol1=solve(eqn)
%Output when variables are specified
sol2 = solve(eqn, [RB L theta])
1 comentario
Steven Lord
el 12 de Dic. de 2023
Imo, it is best to specify the order in which the solution is expected.
I would prefer your last two lines of code, where you don't depend on the ordering of variable names at all. Just let solve return them as fields of a struct array.
syms RB L theta;
%this makes output real numbers
sympref('floatingpointoutput',true);
% here are the equations from the hand set up
e1=RB*cosd(30)+3500*cosd(theta);
e2=-L+3500*sind(theta)-RB*sind(30);
e3=(0.35*L)-(0.25*RB);
eqn = [e1, e2, e3];
sol1=solve(eqn)
As an additional benefit, you can call subs to subtitute values from the struct array back in your equations without having to split the struct into its individual fields.
equation1 = subs(e1, sol1)
That's pretty close to 0.
Más respuestas (0)
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!