Solving non linear equation with a range in MATLAB (problem)

I need to solve below non-linear equation with a range but cannot find a way to do it
Range of (x) is -40 to 30 with increments of 5. (x) is known but I need to find range of (y) and plot (x) vs (y). Please with the answer provide an explanation also. Thank you
p=0.5
S=(sin(y-x)-p*sin(x)*sin(y))=0

 採用された回答

JK
JK 2018 年 2 月 7 日

0 投票

Hi Abdullah,
here is the code with comments:
x=[-40:5:30]'; % x in rad or °? Matlab uses rad
n = size(x,1); % numbers of calculations
yy=zeros(n,1); % preallocation of yy to make it a column-vector
for i=1:n
yy(i)=fsolve(@(y) fun(x(i),y),0); % solve implicit equation
end
figure
plot(x,yy) % plot it
function S=fun(x,y)
S=(sin(y-x)-0.5*sin(x)*sin(y)); % your function
end

4 件のコメント

Walter Roberson
Walter Roberson 2018 年 2 月 7 日
Note that values -40:5:30 are likely intended to be degrees rather than radians.
Abdullah Nasir
Abdullah Nasir 2018 年 2 月 7 日
I have not understood this equation. Kindly can you explain this to me
yy(i)=fsolve(@(y) fun(x(i),y),0);
JK
JK 2018 年 2 月 7 日
yy(i)=fsolve(@(y) fun(x(i),y),0);
  • yy(i) is y(x(i))
  • fsolve is the solver for implicit equations, it solves f(x)=0 for x. You can also use fzero is this case which is probably faster.
  • @(y) fun(x(i),y) is an anonymus function. I uses the x(i) value by the time it is called and solves the function fun(x,y) for y.
  • 0 is the initial guess for y. For a simple equation, 0 will normally suffice as a starting point. If the equation gets more complicated try better starting values.
for using ° instead of rad try
x=[-40:5:30]'/180*pi;
...
plot(x/pi*180,yy/pi*180)

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 2 月 7 日

0 投票

syms x y
p = 0.5;
eq = (sind(y-x)-p*sind(x)*sind(y)) == 0;
Y = simplify(solve(eq, x));
This gives you an explicit formula for Y. You can substitute particular numeric values for x into the formula.
MATLAB will return two expressions. If you analyze the expressions, they turn out to differ by 180 degrees. Further analysis shows that there an an infinite number of other solutions that are 180 apart.

3 件のコメント

Abdullah Nasir
Abdullah Nasir 2018 年 2 月 7 日
編集済み: Walter Roberson 2018 年 2 月 7 日
I have entered below code but I am getting errors
Errors
Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 54)
checkVariables(vars);
Error in solve>getEqns (line 451)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in Untitled13 (line 5)
Y = simplify(solve(eq, x));
Code I entered
syms x y p
p = 0.5;
x=[-40:5:30]
eq = (sind(y-x)-p*sind(x)*sind(y)) == 0;
Y = simplify(solve(eq, x));
Walter Roberson
Walter Roberson 2018 年 2 月 7 日
Do not assign numeric values to x before the code that follows.
xvec = -40:5:30;
syms x y
p = 0.5;
eq = (sind(y-x)-p*sind(x)*sind(y)) == 0;
Y = simplify(solve(eq, y)); %typo fixed on this line
Yn = double( subs(Y, x, xvec) );
plot(xvec, real(Yn));
Abdullah Nasir
Abdullah Nasir 2018 年 2 月 7 日
Thank you

サインインしてコメントする。

質問済み:

2018 年 2 月 7 日

コメント済み:

JK
2018 年 2 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by