# 3rd order approximation solving for variable

5 views (last 30 days)
Jose Iglesias on 22 Mar 2023
Answered: John D'Errico on 22 Mar 2023
I am using a 3rd order approximation to solve the equation shown below:with the x values representing input voltages and y values representing measured output voltages.I need asistance in solving for Vin with Vo given as .956V
Vo = Co + C1*Vin +C2*Vin^2+C3*Vin^3
I solved for Co,C1,C2,and C3 with the following Matlab code
x = (0.2:0.2:0.8);
y = [.3168, .7404, 1.3736, 2.2692];
p = polyfit(x,y,3)
This code gave me the following values which I know are correct.
Co =1.1
C1 = 1.3
C2 = 1.03
C3 = 0.05
I tried to solve for Vin using the following code but it does not give me the correct answer since the Vin is supposed to be Vin = 0.475
equation = [0.05 1.03 1.3 1.1 -.956];
roots(equation)
I do not get Vin = .475 with the previous code and this code gives me 4 different answers for Vin . The original problem statement is shown below. I need assistance in creating the code to solve for Vin. Thanks you in advance!

John D'Errico on 22 Mar 2023
x = (0.2:0.2:0.8);
y = [.3168, .7404, 1.3736, 2.2692];
format long g
p = polyfit(x,y,3)
p = 1×4
1.09999999999999 1.30000000000001 1.02999999999999 0.050000000000001
So, to within floating point trash, those were the coefficients.
However you then did this:
equation = [0.05 1.03 1.3 1.1 -.956];
WRONG!!!!!!!
How many elements are in that vector of coefficients? 5.
numel(equation)
ans =
5
You created a FOURTH degree polynomial.
Do you not see there is a difference between what you did, and this?
equation2 = [0.05 1.03 1.3 1.1-.956];
numel(equation2)
ans =
4
That extra space in there is important. It is best to get into the havit of putting commas vbetween number, and to be CAREFUL about spaces there.
But worse, WHY DID YOU FLIP THE COEFFICIENTS AROUND? Polyfit returns the HIGHEST order coefficient first. In order to do what you want, you need to subtract off from the CONSTANT term. But if you flip the coefficients around, the result will be garbage. You need to do this:
equation3 = [1.1, 1.3, 1.03, 0.05-.956]
equation3 = 1×4
1.1 1.3 1.03 -0.906
And NOW, roots will work.
roots(equation3)
ans =
-0.82932927756341 + 1.0195547804596i -0.82932927756341 - 1.0195547804596i 0.476840373308636 + 0i
Remember, this is a CUBIC polynomial. So there will be three roots. The third root is the one you probably wanted, as it is the real root.

### Categories

Find more on Programming in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by