[Error using char] How do I solve a transcendental equation for given variables?

This is my code which enabled a graph to be plotted.
s = 'F/(11*((0.8*10^-3)^2)*2.41*10^9)= sin(x)*((cos (18.97)/cos (x))-1)^1.5';
ezplot(s , [5 10 15 20])
This is the code which gave the following error.
*Error using char Complex values cannot be converted to chars
Error in solve>getEqns (line 245) vc = char(v);
Error in solve (line 141) [eqns,vars,options] = getEqns(varargin{:});*
//Defining variables
a =5;
b=13;
c=1*10^-3;
d=0.57*10^-3;
e = 0.57*10^-3;
f=0.02*10^-3;
g = d/c+e/c-1;
h = acos(1 - f/(2*g*c));
i = 6*(10^6)*g^2+3*(10^6)*g*6894.75729;
//Transcendental function
k= '(a/(b*(c^2)*i) = (sin(j)*((cos(h))/(cos(j))-1)^1.5)';
ezplot(k, [5 10 15 20]);
The concern I have is the error only appears when I change the value within the function into a defined variable (e.g a,b,c)
What am I doing wrong and how do I correct this?

 Accepted Answer

When you pass a string, values you have set in the workspace are not used. So the "i" you defined as a real value will not be paid attention to and instead "i" will have its default meaning as the imaginary constant, sqrt(-1)
Try
k= @(j) (a/(b*(c^2)*i) - (sin(j)*((cos(h))/(cos(j))-1)^1.5);

13 Comments

The error still appeared if I had not used 'i'. In the example code I tried, I replaced 11 with my variable b(already given the value 11).
I managed to plot the graph in the first function using the number 11, but not when I changed it to 'b'.
s = 'F/(11*((0.8*10^-3)^2)*2.41*10^9)= sin(x)*((cos(18.97)/cos(x))-1)^1.5';
ezplot(s, [5 10 15 20])
b =11;
s = 'F/(b*((0.8*10^-3)^2)*2.41*10^9)= sin(x)*((cos (18.97)/cos (x))-1)^1.5';
ezplot(s, [5 10 15 20])
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> ezplot at 158
fmsg = char(f);
Have you tried using the anonymous function form yet?
Remember what I said about values from the workspace not being used when you pass a string. The string that works for you has two names in it, F and x, so ezplot() plots using ranges for F and x (an implicit plot of two variables.) When you toss in "b" as well, you now have three names, and I don't think ezplot() can handle implicit plots of three variables.
If you were to use the anonymous function form, all values from the workspace would be used.
I need 'b' and other values in the function to be defined variables that are user inputs in my GUI program.
I did try this out and got this message
>> s = @(b) 'F/(b*((0.8*10^-3)^2)*2.41*10^9)= sin(x)*((cos (18.97)/cos (x))-1)^1.5';
>> ezplot(s, [5 10 15 20])
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> ezplotfeval>applyfun at 79
z(i) = feval(f,x(i));
Error in ==> ezplotfeval at 66
z = applyfun(x);
Error in ==> ezplot>ezplot1 at 464
[y,f,loopflag] = ezplotfeval(f,x);
Error in ==> ezplot at 144
[hp,cax] = ezplot1(cax,f{1},vars,labels,args{:});
It does not matter that the values are user inputs, as long as they exist in the workspace at the time the anonymous function is created.
Do not use quoted strings with the anonymous function.
s = @(b) F/(b*((0.8*10^-3)^2)*2.41*10^9) - (sin(x)*((cos (18.97)/cos (x))-1)^1.5);
I got this error after executing the ezplot function again. Thank you for being patient with me thus far.
b=11;
s = @(b) F/(b*((0.8*10^-3)^2)*2.41*10^9) - sin(x)*((cos (18.97)/cos (x))-1)^1.5;
>> ezplot(s, [15 20 5 10]);
??? Error using ==> mupadmex
Error in MuPAD command: Unknown slot "symobj::fromString" [slot]
Error in ==> sym.sym>sym.mrdivide at 273
X = mupadmex('mllib::mrdivide',A.s,B.s);
Error in ==> @(b)F/(b*((0.8*10^-3)^2)*2.41*10^9)-sin(x)*((cos(18.97)/cos(x))-1)^1.5
Error in ==> ezplotfeval at 52
z = feval(f,x(1));
Error in ==> ezplot>ezplot1 at 464
[y,f,loopflag] = ezplotfeval(f,x);
Error in ==> ezplot at 144
[hp,cax] = ezplot1(cax,f{1},vars,labels,args{:});
It is probably trying to send vectors or arrays in. Try
s = @(b) F ./ (b .* ((0.8*10^-3)^2)*2.41*10^9) - sin(x) .* ((cos (18.97) ./ cos (x))-1).^1.5;
I got the same error unfortunately.
>> s = @(b) F ./ (b .* ((0.8*10^-3)^2)*2.41*10^9) - sin(x) .* ((cos (18.97) ./ cos (x))-1).^1.5;
>> ezplot(s , [15 20 5 10])
??? Error using ==> mupadmex
Error in MuPAD command: Unknown slot "symobj::fromString" [slot]
Error in ==> sym.sym>sym.rdivide at 236
X = mupadmex('mllib::zip',A.s,B.s,'mllib::divide');
Error in ==> @(b)F./(b.*((0.8*10^-3)^2)*2.41*10^9)-sin(x).*((cos(18.97)./cos(x))-1).^1.5
Error in ==> ezplotfeval at 52
z = feval(f,x(1));
Error in ==> ezplot>ezplot1 at 464
[y,f,loopflag] = ezplotfeval(f,x);
Error in ==> ezplot at 144
[hp,cax] = ezplot1(cax,f{1},vars,labels,args{:});
What are your F and x there? And are you wanting to plot b versus result, or are you wanting an implicit plot over (say) b and x ?
I intend to plot a graph of F against x.
The values of 'b' and the other numbers in the function are given or calculated variables. Only F and x are unknowns.
The equation gives the relationship between Preload, F and contact angles, x, for ball bearing systems.
I do not know how you have x defined at that point, but
s = @(b,x) F ./ (b .* ((0.8*10^-3)^2)*2.41*10^9) - sin(x) .* ((cos (18.97) ./ cos (x))-1).^1.5;
x is undefined at that point. From the ezplot function, the values of x would range from 15 to 20 on the graph. F ranges from 5 to 10.
I got this error again.
>> b=11;
>> s = @(b,x) F ./ (b .* ((0.8*10^-3)^2)*2.41*10^9) - sin(x) .* ((cos (18.97) ./ cos (x))-1).^1.5;
>> ezplot(s , [15 20 5 10])
??? Error using ==> mupadmex
Error in MuPAD command: Unknown slot "symobj::fromString" [slot]
Error in ==> sym.sym>sym.rdivide at 236
X = mupadmex('mllib::zip',A.s,B.s,'mllib::divide');
Error in ==> @(b,x)F./(b.*((0.8*10^-3)^2)*2.41*10^9)-sin(x).*((cos(18.97)./cos(x))-1).^1.5
Error in ==> ezplotfeval at 54
z = feval(f,x(1),y(1));
Error in ==> ezplot>ezimplicit at 253
u = ezplotfeval(f,X,Y);
Error in ==> ezplot at 153
hp = ezimplicit(cax,f{1},vars,labels,args{:});
I googled online looking for other methods. I came across this "solution" but I did not get how he managed to do it. If he managed to do it.
Is there a point in plotting it over a range of F? The result is linear in F, so the other plots would just be multiples of the plot for F = 1.
If b is known, then it should not be a parameter to s,
s = @(F,x) F ./ (b .* ((0.8*10^-3)^2)*2.41*10^9) - sin(x) .* ((cos (18.97) ./ cos (x))-1).^1.5
However when I go back to your original equations, the cos(18.97) has no apparent foundation. That term seems to be from cos(h) where h is acos() of an expression, and that expression does not work out to 18.97 (it works out to 14/13). The original equations also have no "F" so it is a mystery where that appears.
The original equations down to "k" define an equation in "x" that is dependent upon a number of parameters, including the "b=13". If you use that "b=13" then the equation becomes an equation in one variable, one that involves the solution to quintics (5th order polynomials). There are two branches of solution, one of which appears to be the exact negative of the other.
If, as suggested by your later expression, you allow "b" to be a second variable, then Yes, you get a (pair of) quintics in two variables, which you can thus plot over a range of "b" values. As noted the two quintics are exact negatives, and it also turns out that "b" only appears in them as "b^2" so the results are symmetric around the "b" axis; you can thus plot against non-negative "b" to get a full idea of the shape. If you go out to even moderate "x" such as x=35 then you start getting notable artifacts of round-off, so it is better to do this work in higher precision such as the symbolic toolbox with the number of digits turned up.
The code works according to what I wanted.
The values given in the original equation were arbitrary. The equation remained the same. I should have been more consistent in naming my variables to show a better comparison. This is how the two codes would have looked like.
k = 'a/(11*((0.8*10^-3)^2)*2.41*10^9)= sin(j)*((cos (18.97)/cos (k))-1)^1.5';
ezplot(k , [5 10 15 20])
//This is the code which gave the following error.
*Error using char Complex values cannot be converted to chars
Error in solve>getEqns (line 245) vc = char(v);
Error in solve (line 141) [eqns,vars,options] = getEqns(varargin{:});*
//Defining variables
a =5;
b=13;
c=1*10^-3;
d=0.57*10^-3;
e = 0.57*10^-3;
f=0.02*10^-3;
g = d/c+e/c-1;
h = acos(1 - f/(2*g*c));
i = 6*(10^6)*g^2+3*(10^6)*g*6894.75729;
//Transcendental function
k= '(a/(b*(c^2)*i) = (sin(j)*((cos(h))/(cos(j))-1)^1.5)';
ezplot(k, [5 10 15 20]);
b,c,h,i are known constants.
My objective is to analyse the difference in the plots, given different values of b,c,h or i. To be honest, I'm utilizing the equation from my literature study. So I have to make sense out of it somehow. Haha.
Thank you for your help. I'd be able to move on from here!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!