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 中查找有关 Function Creation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by