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)

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 = 

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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by