Have trouble to solve the equation and plot

Hi,I am new to Matlab,this is my story: I am trying to solve this equation: tan(2.0507*sqrt(x))=(2*sqrt((U-x)*x))/(2*x-U) where U ranges from 2 to 50 for any small steps. And after that I need to plot x vs U. Below is my code:
for U=2:1:50
x=solve('tan(2.0507*sqrt(x))=((2*sqrt((U-x)*x))/(2*x-U))','x');
end
figure
plot(x,U);
After I run this code,there were some warnings like *Warning: Explicit solution could not be found. * kept popping up, and an error about the plot : Vectors must be the same lengths.

Answers (1)

José-Luis
José-Luis on 9 Sep 2012
Edited: José-Luis on 9 Sep 2012
You realize that no matter the value of U (well, except in the cases where 2*x-U=0), the solution of your equation will always be x=0.
Having said that, your code does not work since you need to pass U as a value, not as a string.
your_result = NaN * ones(49,1);
xCoord = 2:1:50;
for U=xCoord
strExpr = ['tan(2.0507*sqrt(x))=((2*sqrt((' num2str(U) ...
'-x)*x))/(2*x-' num2str(U) '))'];
x=solve(strExpr,'x');
your_result(U-1) = eval(x);
end
figure
plot(xCoord,your_result,'k.');
Also, your expression is of the form fun1 = fun2, to which you apply solve. It might be faster (and neater) to input it as fun1 - fun2.

This question is closed.

Products

Asked:

on 9 Sep 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!