フィルターのクリア

Changing the name of a variable changes the results?

2 ビュー (過去 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 Center および File ExchangeCalculus についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by