フィルターのクリア

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 Center および File ExchangeView 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