필터 지우기
필터 지우기

Question about a script

조회 수: 2 (최근 30일)
Elan Hartmann
Elan Hartmann 2013년 11월 18일
편집: Azzi Abdelmalek 2013년 11월 18일
I wrote a script with 3 parts to simulate a hockey puck bouncing around on a plot.
%Part 1: main body of script
hf=figure;
ha=axes('Parent',hf);
hp=plot(ha,0,0,'o','LineWidth',5);
axis([0 5 0 2]);
%set(hp,'XData',1:10)
pos = [0,0]
vel = [1,1]
for step = 1:.02:1000
set (hp,'XData',pos(1))
set (hp,'YData',pos(2))
pos = pos_vel(pos,vel)
[pos,vel]= Reflect(pos,vel)
Part 2: kinematics function to update pos and vel of puck
function [ new_pos ] = pos_vel(pos,vel,dt)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
dt = .5;
new_pos = pos + vel*dt
end
Part 3: keeps puck in boundaries of (x,y) = from (0,0) to (5,2)
function [pos,vel] = Reflect(pos,vel)
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
if pos(1) >= 5
pos(1) = 10-pos(1)
vel(1) = -vel(1)
end
if pos(1)<=0
pos(1) = -pos(1)
vel(1) = -vel(1)
end
if pos(2) >= 2
pos(2) = 4-pos(2)
vel(2) = -vel(2)
end
if pos(2)<= 0
pos(2) = -pos(2)
vel(2) = -vel(2)
end
end
When I try to run I get msg: "Undefined function 'pos_vel' for input arguments of type 'double'.
Error in Lab_13_Revised (line 12) pos = pos_vel(pos,vel)"
Is this a problem with the file path or with the code? I have tried the mkdir and addpath commands and it still is not working.

답변 (1개)

Simon
Simon 2013년 11월 18일
Hi!
You may not mix a script with a function in one file! If you want to write a function called "pos_vel", you have to save it in a separate file called "pos_vel.m". The function file must be on the path. In a script file Matlab cannot call a function defined in the same file!

카테고리

Help CenterFile Exchange에서 View and Analyze Simulation Results에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by