필터 지우기
필터 지우기

Changing the name of a variable changes the results?

조회 수: 3 (최근 30일)
David
David 2023년 12월 12일
댓글: David 2023년 12월 12일
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
RB = 
L
L = 
theta
theta = 
  댓글 수: 2
Rik
Rik 2023년 12월 12일
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.
David
David 2023년 12월 12일
Thank you for the answer. I had one of my colleagues look at this and he confirmed that the variables do have to be in alphabetical order in an equation set like this one. Weird but true!

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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 12월 12일
편집: Dyuman Joshi 2023년 12월 12일
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."
As mentioned by @Rik, L comes before RB, but comes after B alphabetically.
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]
eqn = 
%symvar on the equations
symvar(eqn)
ans = 
%Output when variables are not specified
sol1=solve(eqn)
sol1 = struct with fields:
L: [2×1 sym] RB: [2×1 sym] theta: [2×1 sym]
%Output when variables are specified
sol2 = solve(eqn, [RB L theta])
sol2 = struct with fields:
RB: [2×1 sym] L: [2×1 sym] theta: [2×1 sym]
  댓글 수: 1
Steven Lord
Steven Lord 2023년 12월 12일
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)
sol1 = struct with fields:
L: [2×1 sym] RB: [2×1 sym] theta: [2×1 sym]
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)
equation1 = 
That's pretty close to 0.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by