I am trying to find the roots of this equation but my code doesn't work. It keeps saying theta0 is unrecognized,

1 回表示 (過去 30 日間)
fun = @f;
x0 = [0 6.28];
z = fzero(fun,x0)
function theta0 = f(y0,yf,v0,x)
y0= 2;
yf= 1;
v0=20;
x=35;
theta0= x*tan(theta0)-4.905*(x^2)/((v0^2)*((cos(theta0))^2))-(y0-yf);
end

回答 (3 件)

Arif Hoq
Arif Hoq 2022 年 3 月 1 日
編集済み: Arif Hoq 2022 年 3 月 1 日
save your function. you have to define your variable theta0 in this function.
function y = f(x)
y0= 2;
yf= 1;
v0=20;
theta=35;
y= x*tan(theta0)-4.905*(x^2)/((v0^2)*((cos(theta0))^2))-(y0-yf);
end
then call this function in a diffrent script(m file)
fun = @f;
x= [0 6.28];
z = fzero(fun,x)
  1 件のコメント
Anita Osoh
Anita Osoh 2022 年 3 月 2 日
x is a value in the function. I am solving for theta. That is why it has no variable.

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


John D'Errico
John D'Errico 2022 年 3 月 1 日
It appears that your equation is:
theta0= x*tan(theta0)-4.905*(x^2)/((v0^2)*((cos(theta0))^2))-(y0-yf);
and you wish to solve for x that satisfies this implicit function of x, where theta0 = 35 is given also?
The trick is to write it as a problem where the function would be zero. Do that by subtracting theta0 from both sides. Now your problem looks like:
x*tan(theta0)-4.905*(x^2)/((v0^2)*((cos(theta0))^2))-(y0-yf) - theta0 == 0
Now it is time to write some MATLAB code. We should verify it has any solution at all.
y0 = 2;
yf = 1;
v0 = 20;
theta0 = 35;
F = @(x) x*tan(theta0)-4.905*(x.^2)/((v0^2)*((cos(theta0))^2))-(y0-yf) - theta0;
PLOT IT!!!!
fplot(F,[-100,100])
So your problem, if it is assumed to be a function of x, has NO point where it crosses zero.
As such fzero would fail, although solve would find there are a pair of complex roots, since this is just a quadratic equation. THus
syms x
vpasolve(x*tan(theta0)-4.905*(x.^2)/((v0^2)*((cos(theta0))^2))-(y0-yf) - theta0)
ans = 
  1 件のコメント
Anita Osoh
Anita Osoh 2022 年 3 月 2 日
x is a value in the function. I am solving for theta. That is why it has no variable.

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


Torsten
Torsten 2022 年 3 月 2 日
function main
theta0 = 0;
theta = fzero(@f,theta0)
end
function res = f(theta)
y0 = 2;
yf = 1;
v0 = 20;
x = 35;
res = x*tan(theta)-4.905*(x^2)/((v0^2)*((cos(theta))^2))-(y0-yf);
end

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by