Intersection between two functions

485 ビュー (過去 30 日間)
Behbod Izadi
Behbod Izadi 2021 年 9 月 8 日
コメント済み: Star Strider 2024 年 4 月 25 日 4:04
Hi, fairly new to Matlab. Struggling with a question requiring me to find an x value for which:
2.2/sqrt(2*9.81*x) = tanh((3.5/(2*4.5))*sqrt(2*9.81*x)
Tried to use finding an intersection between two functions in accordance with another answer on this website, but I get multiple errors, both in graphing the function to see roughly where the correct solution should be and in finding a solution at all for the intersection. I also tried using symbolic variables (not sure what the difference is to be honest), but couldn't get it to work.
I also get this error at the very start:
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize
your function to return an output with the same size and shape as the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function/FunctionLine/updateFunction
In matlab.graphics.function/FunctionLine/set.Function_I
In matlab.graphics.function/FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 245)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 200)
In fplot>vectorizeFplot (line 200)
In fplot (line 166)
In question (line 7)
Error using /
Matrix dimensions must agree.
Error in question (line 15)
f1a= 2.2/(sqrt(2*9.81*x));
This is my code at the moment:
%input array
x=linspace(0,1,1000);
f1=@(x) 2.2/sqrt(2*9.81*x);
f2=@(x) tanh((3.5/(2*4.5))*sqrt(2*9.81*x));
% Graph functions
fplot(f1,'b')
hold on
fplot(f2,'r')
grid on
title('Finding Intersections of Functions')
xlabel('Input Values (x)')
ylabel('Ouput Values (f)')
% Find the x-cordinates of intersecting points
f1a= 2.2/(sqrt(2*9.81*x));
f2a= tanh((3.5/(2*4.5))*sqrt(2*9.81*x));
Intersections=find(abs(f1a-f2a)<=(0.0001));
X_Values= x(Intersections)
Would appreciate any help.

回答 (3 件)

Star Strider
Star Strider 2021 年 9 月 8 日
Try this slightly changed version of the existing code —
f1=@(x) 2.2./sqrt(2*9.81*x);
f2=@(x) tanh((3.5/(2*4.5))*sqrt(2*9.81*x));
% Graph functions
figure
fplot(f1,'b')
hold on
fplot(f2,'r')
grid on
title('Finding Intersections of Functions')
xlabel('Input Values (x)')
ylabel('Ouput Values (f)')
% Find the x-cordinates of intersecting points
Intersections = fzero(@(x) f1(x)-f2(x), 1)
Intersections = 0.3924
X_Values= Intersections
X_Values = 0.3924
Y_Values = f1(Intersections) % Can Use Either Function, since this is common to both
Y_Values = 0.7929
plot(X_Values, Y_Values, 'sg', 'MarkerSize',10)
hold off
Experiment to get different results.
.
  2 件のコメント
Zihao Hu
Zihao Hu 2024 年 4 月 25 日 2:15
what does the "1' mean in "Intersections = fzero(@(x) f1(x)-f2(x), 1)"
Thank you!
Star Strider
Star Strider 2024 年 4 月 25 日 4:04
@Zihao Hu — The ‘1’ is the initial guess for ‘x’. The fzero function (and other such functions) need an initial guess for each parameter being solved for. (It is always best to use something other than zero for that value.)

サインインしてコメントする。


Chien Poon
Chien Poon 2021 年 9 月 8 日
All you need is the element wise division (adding ./) on variable f1a
%input array
x=linspace(0,1,1000);
f1=@(x) 2.2/sqrt(2*9.81*x);
f2=@(x) tanh((3.5/(2*4.5))*sqrt(2*9.81*x));
% Graph functions
fplot(f1,'b')
hold on
fplot(f2,'r')
grid on
title('Finding Intersections of Functions')
xlabel('Input Values (x)')
ylabel('Ouput Values (f)')
% Find the x-cordinates of intersecting points
f1a= 2.2./(sqrt(2*9.81*x));
f2a= tanh((3.5/(2*4.5))*sqrt(2*9.81*x));
Intersections=find(abs(f1a-f2a)<=(0.0001));
X_Values= x(Intersections)
  2 件のコメント
Elina Nikolopoulou
Elina Nikolopoulou 2021 年 12 月 28 日
how do i find the y-coordinates of the intersecting points ?
Star Strider
Star Strider 2021 年 12 月 28 日
@Elina Nikolopoulou By using the approach in my Answer!

サインインしてコメントする。


William Rose
William Rose 2021 年 9 月 8 日
syms x
S=solve(2.2/sqrt(2*9.81*x) == tanh((3.5/(2*4.5))*sqrt(2*9.81*x)),x)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
S = 
gives an answer. Is it reasonable? The solution x=-212 means you are taking the square root of a negative number, which has an imaginary result. Is that acceptable to you? vpasolve returns the first solution it finds. I wonder if there is a solution involivng a non-negative value of x, which vpasolve did not find. Let's plot to find out.
x=0:.1:10;
figure;
plot(x,2.2./sqrt(2*9.81*x),'.r-',x,tanh((3.5/(2*4.5))*sqrt(2*9.81*x)),'.b-');
xlabel('x'); ylabel('f(x)'); legend('sqrt','tanh');
Note that in the plot command, I had to use './sqrt()' rather than '/sqrt()', to avoid an error. Note also that the red plot had a value of y=Inf when x=0, and that was not a problem; the plot function just skips that point. The plot shows that there IS a non-negative x which is a solution, and it appears to be between x=0.3 and 0.4, very close to x=0.4. We can tell vpasolve() to look for a solution in the range [0 10], as follows:
syms x
S=vpasolve(2.2/sqrt(2*9.81*x) == tanh((3.5/(2*4.5))*sqrt(2*9.81*x)),x,[0 10])
S = 
0.39242469611262814472271476457847
This value is consistent with the plot.

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by