convert equation to code

10 views (last 30 days)
chris
chris on 8 Sep 2022
Edited: John D'Errico on 8 Sep 2022
i am trying to conver the below equation to code so that i can graph it.
i receive
Warning: Imaginary parts of complex X and/or Y arguments ignored.
> In untitled3 (line 3)
i beleive this to be due to a poor conversion of the eqution on my behalf.
the equation is
120/pi*acos(2v-80/10-v) where V is subsitiute for t2. i have set t2 t2=30:0.01:70;
i have enetrered
y2=(120/pi)*acos((2*t2)-80)/(((10-t2)));
any ideas?

Answers (1)

John D'Errico
John D'Errico on 8 Sep 2022
Edited: John D'Errico on 8 Sep 2022
Your problem is that the cosine function produces only numbers between -1 and 1. That is another way of saying that acos ONLY works on numbers that are between -1 and 1. Remember that acos is the functional inverse of the cosine function.
If you give it (acos) a number outside of that interval, then you get a complex number as a result. For example:
acos(1.2)
ans = 0.0000 + 0.6224i
Do you see that I got a complex result? Now we can look at what you did. When you have t2 as:
t2=30:0.01:70;
Now, what is:
acosarg = (2*t2)-80;
min(acosarg)
ans = -20
max(acosarg)
ans = 60
Do you see this produces numbers that are far outside of the interval that acos can handle?
For some reason, you seem to be thinking that acos uses degrees. Even radians are not meaningful here, in terms of the ARGUMENTS to acos. Again, look at the plot below:
fplot(@acos,[-1,1])
As you can see, acos lives on the interval [-1,1]. Go beyond that, and you get complex stuff as a result.
Next, even when you do get the arguments to acos correct, you will need to learn about the use of the ./ operator.
When you want to multiply or divide the elements of two vectors or arrays, you need to use .* to ./ to perform that operation. Otherwise, MATLAB will have a problem.
So if t2 is a vector, then even if this line of code was written correctly for scalars, with a vector t2, this line of code SHOULD have been written:
y2=(120/pi)*acos((2*t2)-80)./(((10-t2)));
Do you see that I used a ./ operator there? You can multiply a scalar times a vector, because MATLAB is smart enough to do that operation. You can even divide the elements of a vector by a scalar. But you CANNOT divide the elements of two vectors in an element-wise fashion without using the ./ operator.

Tags

Products

Community Treasure Hunt

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

Start Hunting!