MATLAB Answers

How to solve the erro about fzero function?

조회 수: 2(최근 30일)
ryunosuke tazawa
ryunosuke tazawa 2021년 7월 22일
댓글: Rik 2021년 7월 23일
I am making code that simple pendulum throwing ball to target point.
The simple pendulum is controlled by adding a constant torque to the equation of motion.
I have defined a function to find the difference between the target point and the arrival point of the ball.
Torque is the input variable for that function.
Therefore, I tried to find the torque that makes the target and the ball reach 0 using fzero, but the following error occurred.
What does this error mean?
What should I do to fix it?  Please someone teach me.
error: fzero (行 307)
elseif ~isfinite(fx) || ~isreal(fx)
error: hairetu (行 8)
Trq = fzero(fun,Trq0)
clear ;
close all;
clc;
fun = @fun_ball
Trq0 = 10;
Trq = fzero(fun,Trq0)
%% function for mesuaring distance between ball arivin point and target-point
function ball_gosa = fun_ball(Trq)
g = 9.81; l = 1; % g is gravity , l is length of pendulum
theta_ic = [0; 0]; tspan = [0 2]; % theta_ic is initial value for (Angle angular velocity)
[t, theta] = ode45(@(t,theta) ode_function(t,theta,g,l,Trq),tspan,theta_ic); % slove the Equation of motion using ode
w = theta(:,2); theta = theta(:,1);
ball_x =1*sin(theta); ball_y = -1*cos(theta); % point of throwing ball
ball_time = sqrt(2*abs(ball_y)/g); % time of ball fall
ball_reach =ball_x + abs(w).*ball_time; % distance of ball's arrival point
ball_target = 1.5; % target point
ball_gosa = ball_target - ball_reach; % Distance difference between the arrival point of the ball and the target
end
%% simple pendulum
function [dtheta_dt] = ode_function(t, theta,g,l,Trq)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt =-(g/l)*(theta1)-Trq;
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end

답변(1개)

Rik
Rik 2021년 7월 22일
Your custom function should return a scalar value, but it doesn't:
fun = @fun_ball;
Trq0 = 10;
fun(Trq0)
ans = 65×1
1.5000 1.5000 1.5000 1.4999 1.4999 1.4998 1.4997 1.4996 1.4995 1.4989
  댓글 수: 5
Rik
Rik 2021년 7월 23일
You are looking for a zero of your function. How certain are you that it exists?
Because fzero only found positive values, you could try fminsearch to find the lowest point in your function and see how close it is to 0.
fun = @fun_ball;
Trq0 = 10;
%Trq = fzero(fun,Trq0)
x_min=fminsearch(fun,Trq0);
fun(x_min)
ans = 2.2484
%% function for mesuaring distance between ball arivin point and target-point
function ball_gosa = fun_ball(Trq)
g = 9.81; l = 1; % g is gravity , l is length of pendulum
theta_ic = [0; 0]; tspan = [0 2]; % theta_ic is initial value for (Angle angular velocity)
[t, theta] = ode45(@(t,theta) ode_function(t,theta,g,l,Trq),tspan,theta_ic); % slove the Equation of motion using ode
w = theta(:,2); theta = theta(:,1);
ball_x =1*sin(theta); ball_y = -1*cos(theta); % point of throwing ball
ball_time = sqrt(2*abs(ball_y)/g); % time of ball fall
ball_reach =ball_x + abs(w).*ball_time; % distance of ball's arrival point
ball_target = 1.5; % target point
ball_gosa = max(ball_target - ball_reach); % Distance difference between the arrival point of the ball and the target
% ^^^^ this is my edit ^
end
%% simple pendulum
function [dtheta_dt] = ode_function(t, theta,g,l,Trq)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt =-(g/l)*(theta1)-Trq;
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
So, it turns out your function doesn't have a 0 close to Trq0.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by