Intersection between two functions
435 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (3)
Star Strider
el 8 de Sept. de 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.
.
0 comentarios
Chien Poon
el 8 de Sept. de 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 comentarios
Elina Nikolopoulou
el 28 de Dic. de 2021
how do i find the y-coordinates of the intersecting points ?
William Rose
el 8 de Sept. de 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 comentarios
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!