How would I write a code to solve a system of equations?

I need to solve a system of equations but I do not know the syntax for doing so. The equations are:
q1 = (1.53*10^-5)*t1^4 + (1.67*10^-2)*t1^3 + 6.85*t1^2 + 2746*t1 - 57793
q2 = 13.3*(t1-t2)
q3 = (1.53*10^-5)*t2^4 + (1.67*10^-2)*t2^3 + 6.85*t2^2 + 4846*t2 - 49161
also; q1=q2=q3
I'm new to MATLAB and not sure how to handle this many variables along with the scientific notation.

2 Comments

do you have to use a mathematical method or any way would be fine?
I don't know what you mean exactly. How would you solve this in a non-mathematical way?

Sign in to comment.

Answers (4)

Dear Omar, you can solve your system of equations using the following way:
t = sym('t%d', [1 2]);
q1 = 1.53e-5 * t(1)^4 + 1.67e-2 * t(1)^3 + 6.85 * t(1)^2 + 2746 * t(1) - 57793;
q2 = 13.3 * (t(1) - t(2));
q3 = 1.53e-5 * t(2)^4 + 1.67e-2 * t(2)^3 + 6.85 * t(2)^2 + 4846 * t(2) - 49161;
[solutions_t1, solutions_t2] = solve(q1 == q2 == q3, t(1), t(2))
Or
[solutions_t1, solutions_t2] = solve(q1 == q2, q2 == q3, t(1), t(2))
You can check which solutions are better because in first case you will get 4 solutions for both t1 and t2 and in second case you will get 16 solutions for both t1 and t2. I hope it helps. Good luck!

25 Comments

I don't know that q1 == q2 == q3 would work. You might need {q1 == q2, q1 == q3})
Thanks for suggestion. I checked the alternate approach as well and edited my answer.
I get a list of errors from the code you gave:
??? Error using ==> char Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 189 vc = char(v);
Error in ==> solve at 67 [eqns,vars] = getEqns(varargin{:});
Error in ==> Untitled at 5 [solutions_t1, solutions_t2] = solve(q1 == q2, q2 == q3, t(1), t(2));
Do you have Symbolic Math toolbox installed?
I'm not sure. I have version 7.12.0 if that helps.
In the command window write
ver
It will show you the list of toolboxes you have in your MATLAB. In that list look for "Symbolic Math Toolbox"
yes, I have it. Version 5.6
Then the code should run properly. I tested it. Once try to clear your Wrokspace variables using "clear all" command and then try to run the code again
I'm getting the same error. What is the conversion to char error referencing?
I'm not getting any error and getting following set of solutions:
solutions_t1 =
20.042806342995599627414326408957
-817.99430727766422040312071627286
-819.52640166626545459448871351105
24.303486382747843485823623789841
- 818.11077073383417867538277932297 + 0.89940540009597370152161161708158*i
- 148.14017634295369511044919987683 + 458.3397547015855215537971645283*i
- 146.77588352654392288758538393655 + 456.4253253675986002392010409508*i
- 148.14017634295369511044919987683 - 458.3397547015855215537971645283*i
- 145.77037408650156329558831379242 + 457.37640076434990571556994854342*i
- 145.77037408650156329558831379242 - 457.37640076434990571556994854342*i
20.390004081959019893522201721883 - 2.5096839659993983377298976991764*i
- 818.11077073383417867538277932297 - 0.89940540009597370152161161708158*i
20.390004081959019893522201721883 + 2.5096839659993983377298976991764*i
- 146.77588352654392288758538393655 - 456.4253253675986002392010409508*i
- 148.01212723074507617500219139259 + 455.76612217244289354202712428084*i
- 148.01212723074507617500219139259 - 455.76612217244289354202712428084*i
solutions_t2 =
10.026534951396692406197841436985
7.7908234307870665723377273048482
-970.19099147939542216018471301849
-970.84377569323430243011326603826
- 64.551549934232581000123010021898 + 571.71255928313319163169830940455*i
- 970.71087979831249399045253798314 - 0.35421398207746943169552341282171*i
9.5848334989441735758721476971283 + 1.2152474990061433017004916662752*i
- 970.71087979831249399045253798314 + 0.35421398207746943169552341282171*i
- 65.548269631771464366207121989621 + 571.80819802644128811606067612527*i
- 65.548269631771464366207121989621 - 571.80819802644128811606067612527*i
- 65.343013895264786417371166055657 + 572.37362181482009597129417516613*i
- 64.551549934232581000123010021898 - 571.71255928313319163169830940455*i
- 65.343013895264786417371166055657 - 572.37362181482009597129417516613*i
9.5848334989441735758721476971283 - 1.2152474990061433017004916662752*i
- 64.828951791852397188508512881383 - 572.66923143390374147395907403948*i
- 64.828951791852397188508512881383 + 572.66923143390374147395907403948*i
This is odd. None of the real solutions seem reasonable but I know that the equations are correct. I think it's treating each equation like it is set equal to zero. Which is not correct. How would you find the solution for q?
I figured out how to get rid of the char error that was causing a lot of issue. Here is the code I wrote:
clear;
clc;
syms x y;
q1 = 1.53e-5 * x^4 + 1.67e-2 * x^3 + 6.85 * x^2 + 2746 * x - 57793;
q2 = 13.3 * (x - y);
q3 = 1.53e-5 * y^4 + 1.67e-2 * y^3 + 6.85 * y^2 + 4846 * y - 49161;
q1 = q2;
q2 = q3;
[solutions_x, solutions_y, solutions_q1] = solve('x', 'y', 'q1');
I'm still not getting solutions though. Now that I look back on the derivations of the equations. I'm certain that the program is treating each equation like it was set equal to 0. How do I fix that?
Convert
solve(q1 == q2, q2 == q3, t(1), t(2));
to
solve(q1 - q2, q2 - q3, t(1), t(2))
It is still claiming that an explicit solution cannot be found. What concerns me is that six's code apparently works (at least in the sense that he gets an output). So why can can't I get a solution with the exact same code? Even when I put single quotes around each item, it still won't give me a solution.
Omar the problem is that you have one equation eventually due to condition q1 = q2 = q3 and you have two unknowns t1 and t2. So, in this case explicit solution don't exist and there could be infinite set of solutions mathematically. So may be you can think again about the condition q1 = q2 = q3
I don't know why that would be. There are 3 equations and 3 unknowns. q1, q2, and q3 are actually the same thing (meaning that 1 variable is being called 3 different things). The unknowns are q, t1, and t2.
When you put q1 = q2 = q3 and then you solve then you will get one equation in which you will have two variables t1 and t2. So for any value of t1 you will get corresponding value for t2. for example for this code line:
[solutions_t1, solutions_t2] = solve(q1 == q2 == q3, t(1), t(2))
I get the following solution and MATLAB uses the mathematical approach which i just described above:
solutions_t1 =
20.086842601695534071659582222863
-818.0065723267874137510207732586
- 146.79176912438219320848725608014 + 456.44240227248617435061388081659*i
- 146.79176912438219320848725608014 - 456.44240227248617435061388081659*i
solutions_t2 =
0
0
0
0
As you can see here MATLAB assumes value 0 for t2(four 0s because of polynomial of degree 4) and computes the values for t1 because here you have just one equation for t1 and t2. So if you will assume different set of values for t2 then you will get different set of values for t1 and in this way there will be infinite solutions to this problem.
So, I guess the question is, how do I write a code such that I get solutions for q, t1, and t2 without explicitly stating that the equations are equal to each other? Also, even with the code I have and the code that was previously posted, I still haven't gotten an output. In this example that you gave, MATLAB still gave you solutions but mine just gives me errors.
Before solving further I would like to ask what do you mean by 'q' here? because t1 and t2 are variables for which solution have to be found but I can't understand meaning of 'q' here. Can you explain a bit?
sure. Essentially, these equations represent the heat transfer through a concrete slab. t1 is the inside surface temperature, t2 is the outside surface temperature, and q is the rate of heat transfer.
q1 is the heat transfer to the slab by convection and radiation.
q2 is the heat transfer to through the slab by conduction.
q3 is the heat transfer to the outside by convection and radiation.
Due to continuity, q is the same throughout the system (hence q1=q2=q3). In other words, q1, q2, and q3 represent the same number which I am calling q.
Therefore:
q = 1.53e-5 * t(1)^4 + 1.67e-2 * t(1)^3 + 6.85 * t(1)^2 + 2746 * t(1) - 57793;
q = 13.3 * (t(1) - t(2));
q = 1.53e-5 * t(2)^4 + 1.67e-2 * t(2)^3 + 6.85 * t(2)^2 + 4846 * t(2) - 49161;
Here is the code for your described problem:
syms t1 q1 t2 q3
q1 = 1.53e-5 * t1^4 + 1.67e-2 * t1^3 + 6.85 * t1^2 + 2746 * t1 - 57793;
solutions_t1 = double(solve(q1, t1)) % Solutions for t1 only from first equation
q3 = 1.53e-5 * t2^4 + 1.67e-2 * t2^3 + 6.85 * t2^2 + 4846 * t2 - 49161;
solutions_t2 = double(solve(q3, t2)) % Solutions for t2 only from third equation
q = 13.3 * (solutions_t1 - solutions_t2) % Solutions for q using solutions for t1 and t2
I don't get any errors now but I still don't get an output in the command window. I think this code will work but how do I get it to show solutions.
You can use:
disp(solutions_t1)
disp(solutions_t2)
disp(q)
Or you can also see your solutions in the Workspace where your variables are stored
ok I got what I'm looking for. Thank you for your help.

Sign in to comment.

{q = 2003.839045, t1 = -819.5264017, t2 = -970.1909915},
{q = -10982.94224, t1 = -817.9943073, t2 = 7.790823431},
{q = 133.2164095, t1 = 20.04280634, t2 = 10.02653495},
{q = 13235.45859, t1 = 24.30348638, t2 = -970.8437757}
provided that you only want real-valued solutions.
This was done by substituting t1 for t(1) and t2 for t(2), and then plugging the three q equations into solve()
David
David on 21 Oct 2013
Edited: David on 21 Oct 2013
In addition to the answers already posted, be sure to put sym() around each of your equations. That is to say, do the following:
syms t1 t2
q1 = sym( 1.53e-5*t(1)^4 + 1.67e-2*t(1)^3 + 6.85*t(1)^2 + 2746*t(1) - 57793 );
q2 = sym( 13.3*(t(1) - t(2)) );
q3 = sym( 1.53e-5*t(2)^4 + 1.67e-2*t(2)^3 + 6.85*t(2)^2 + 4846*t(2) - 49161 );
[solutions_t1, solutions_t2] = solve(q1 == q2, q2 == q3, t1, t2);
solve requires that your equations be symbolic objects which is guaranteed by using sym().

2 Comments

As long as anything in the equation is sym, the whole expression will be treated as sym. So
t = sym('t%d', [1 2]);
is enough to make the equations sufficiently sym() without requiring sym() around the equations.
Oh? This seemed to correct a problem I was having earlier in using the solve function. You actually saw that question and made useful suggestions, though suggestions that did not quite do the trick. Maybe we are both missing something or maybe it's just me...

Sign in to comment.

how to do least square estimation for system with the prbs as input to it

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 19 Oct 2013

Answered:

on 1 Dec 2020

Community Treasure Hunt

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

Start Hunting!