solve x as function of y
Show older comments
Hello I am really stock on this code.
what I am trying to do is basically to find x value when i input y value, for example in linear equation y= x+b this is simple to do. but for polynomial equation is very hard to me.
This is my equation: I am trying to find Po2a1, when I put any value for So21. for example if i input 97.5 for So21, Po2a1 should be 100.
So21= 100* ((-8.5322289*10^3.*Po2a1) + (2.1214010*10^3.*(Po2a1.^2)) + (-6.7073989*10^1.*(Po2a1.^3)) + (Po2a1.^4))./((9.3596087*10^5)+ (-3.1346258*10^4.*Po2a1) + (2.3961674*10^3.*(Po2a1.^2)) + (-6.7104406*10^1.*(Po2a1.^3)) + (Po2a1.^4));
Answers (2)
Star Strider
on 23 Mar 2021
Try this:
Po2a1 = linspace(0, 150);
So21= 100* ((-8.5322289*10^3.*Po2a1) + (2.1214010*10^3.*(Po2a1.^2)) + (-6.7073989*10^1.*(Po2a1.^3)) + (Po2a1.^4))./((9.3596087*10^5)+ (-3.1346258*10^4.*Po2a1) + (2.3961674*10^3.*(Po2a1.^2)) + (-6.7104406*10^1.*(Po2a1.^3)) + (Po2a1.^4));
So21_975 = interp1(So21, Po2a1, 97.5) % Direct Interpolation
So21_fcn = @(x) interp1(So21, Po2a1, x); % Anonymous Function For Interpolation
So21_975 = So21_fcn(97.5) % Anonymous Function Interpolation
Both produce:
So21_975 =
100.18
.
2 Comments
Star Strider
on 23 Mar 2021
A loop would be required with the fzero function:
So21_fcn = @(x) fzero(@(Po2a1) x - 100* ((-8.5322289*10^3.*Po2a1) + (2.1214010*10^3.*(Po2a1.^2)) + (-6.7073989*10^1.*(Po2a1.^3)) + (Po2a1.^4))./((9.3596087*10^5)+ (-3.1346258*10^4.*Po2a1) + (2.3961674*10^3.*(Po2a1.^2)) + (-6.7104406*10^1.*(Po2a1.^3)) + (Po2a1.^4)), 50);
Po2a1_vct = [90; 95; 97.5; 99];
for k = 1:numel(Po2a1_vct)
So21v(k,:) = So21_fcn(Po2a1_vct(k));
end
with:
Result = table(Po2a1_vct, So21v)
producing:
Result =
4×2 table
Po2a1_vct So21v
_________ ______
90 57.65
95 74.534
97.5 100.18
99 156.6
.
Star Strider
on 25 Mar 2021
The fzero function finds whatever it is told is the zero-crossing. That is not necessarily zero, and here it is the value of ‘x’ in my code, so the ‘zero’ is where the function equals ‘x’ that here is the desired value of ‘So21’. .
A much easier and more computationally efficient solution is the first code that I posted, using interp1.
For example:
Po2a1 = linspace(0, 150);
So21= 100* ((-8.5322289*10^3.*Po2a1) + (2.1214010*10^3.*(Po2a1.^2)) + (-6.7073989*10^1.*(Po2a1.^3)) + (Po2a1.^4))./((9.3596087*10^5)+ (-3.1346258*10^4.*Po2a1) + (2.3961674*10^3.*(Po2a1.^2)) + (-6.7104406*10^1.*(Po2a1.^3)) + (Po2a1.^4));
So21_vct = [50:10:90 97.5 98];
Po2a1_Intrp = interp1(So21, Po2a1, So21_vct); % Direct Interpolation
with:
Result = table(So21_vct(:), Po2a1_Intrp(:))
producing:
Result =
7×2 table
Var1 Var2
____ ______
50 26.839
60 31.659
70 37.293
80 44.645
90 57.653
97.5 100.18
98 111.23
and no explicit loops necessary.
David Hill
on 23 Mar 2021
y=10:100;%whatever y values you want [50 60 70 90 97.5]
for k=1:length(y)
eqn=@(x)100*(-8.5322289e3.*x + 2.1214010e3*x.^2 + -6.7073989e1*x.^3 + x.^4)./(9.3596087e5+ -3.1346258e4*x + 2.3961674e3*x.^2 + -6.7104406e1*x.^3 + x.^4)-y(k);
x(k)=fzero(eqn,50);
end
1 Comment
David Hill
on 25 Mar 2021
You subtract so21 so that the equation should equal zero. Did you not bother to look.
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!