How to change values for variables that are defined in a equation

45 views (last 30 days)
Hello everybody!
I have an equation depending on several variables (a, b, c, d) . By defining the values new and wanting the adapted output, it still takes the values for the variables that I defined first (for fist and second set the output is x=1.4321). How can I ressolve that? I dont want to change the names of the variables as I have more than 80 different values for the same variable.
See my code:
%first set of values for variables
a = 75; %q_inc
b = 13.8; %q_losses
c = 73; %rho_char
d = 1.02; %betta_char
syms x
eqn = (a - b + 6.96*x + (6-(31*c)/1000)*x/60*1000 + 31*c/1000*d/60*1000)*0.0081 - 0.1824 -x == 0;
vpasolve(eqn,x)
ans = 1.4231
%second set of values for variables
a = 96.4; %q_inc
b = 14.1; %q_losses
c = 100.6; %rho_char
d = 1.32; %betta_char
vpasolve(eqn,x)
ans = 1.4231

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2021
syms a b c d
syms x
eqn = (a - b + 6.96*x + (6-(31*c)/1000)*x/60*1000 + 31*c/1000*d/60*1000)*0.0081 - 0.1824 -x == 0;
sol = solve(eqn, x)
sol = 
A = [75, 96.4];
B = [13.8, 14.1];
C = [73, 100.6];
D = [1.02, 1.32];
subs(sol, {a,b,c,d}, {A,B,C,D})
ans = 

More Answers (2)

Alan Stevens
Alan Stevens on 16 Apr 2021
Since your equation is linear in x it's probably best to rewrite as in the following, and put all your a,b,c,d vaues into vectors:
a = [75, 96.4];
b = [13.8, 14.1];
c = [73, 100.6];
d = [1.02, 1.32];
x = ((a - b + 31*c/1000.*d/60*1000)*0.0081 - 0.1824)./(1 - 0.0081*(6.96 + (6-31*c/1000)/60*1000));
disp(x)
1.4231 1.8750
  1 Comment
Delia Bosshart
Delia Bosshart on 19 Apr 2021
Thank you Alan for your answer! Unfortunately my equation will look like this in a further step:
eqn2 = (a - b + 6.96*x + (6-(31*232.87*((x-e)*t)^(-0.46))/1000)*x/60*1000 + 31*d/1000*e/60*1000)*0.0081 - 0.1824 -x == 0;
The equation in not linear anymore and I can't solve it by hand anymore which is why i will need the solve function.
I tried this:
x = sym('x', [1 2]);
vpasolve(eqn2, x);
But then the output gives me a struct of names (x1, x2) instead of the values of x1 and x2

Sign in to comment.


Alan Stevens
Alan Stevens on 19 Apr 2021
More like this perhaps (of course you will need to use your on values for the constants):
a = [75, 96.4];
b = [13.8, 14.1];
c = [73, 100.6];
d = [1.02, 1.32];
e = [0.1, 0.2]; t = [0.3, 0.4]; % replace with your values
syms x
for i = 1:numel(a)
eqn2 = (a(i) - b(i) + 6.96*x + (6-(31*232.87*((x-e(i))*t(i))^(-0.46))/1000)*x/60*1000 + 31*d(i)/1000*e(i)/60*1000)*0.0081 - 0.1824 -x == 0;
y = vpasolve(eqn2,x);
disp(y)
end

Tags

Products

Community Treasure Hunt

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

Start Hunting!