MATLAB Answers

Intersection between two functions

조회 수: 4(최근 30일)
Behbod Izadi
Behbod Izadi 2021년 9월 8일
답변: William Rose 2021년 9월 8일
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.
.

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)

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.

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by