Intersection between two functions
152 views (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
Star Strider
on 8 Sep 2021
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)
X_Values= Intersections
Y_Values = f1(Intersections) % Can Use Either Function, since this is common to both
plot(X_Values, Y_Values, 'sg', 'MarkerSize',10)
hold off
Experiment to get different results.
.
2 Comments
Zihao Hu
on 25 Apr 2024
what does the "1' mean in "Intersections = fzero(@(x) f1(x)-f2(x), 1)"
Thank you!
Star Strider
on 25 Apr 2024
@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
on 8 Sep 2021
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 Comments
William Rose
on 8 Sep 2021
syms x
S=solve(2.2/sqrt(2*9.81*x) == tanh((3.5/(2*4.5))*sqrt(2*9.81*x)),x)
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])
This value is consistent with the plot.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!