From: Bob felleo on 10 Jul 2010 05:47 i have a program that shows floating redballs i need a matlab program that would include gravity in this if you could change it to be the apportion answer i would be very great full %redball.m classdef redball < handle properties x; % x position y; % y position vx; % velocity in x vy; % velocity in y r; % radius end properties(Constant) minx = -10; maxx = 10; miny = -10; maxy = 10; end methods function obj = redball( ) obj.x = 18*rand(1) - 9; obj.y = 18*rand(1) - 9; obj.vx = .9*rand(1)-.45; obj.vy = .9*rand(1)-.45; obj.r = rand(1)*.9 + .1; end function [xpts ypts] = getpts(obj) t = 0:0.1:(2*pi); xpts = obj.r*cos(t) + obj.x; ypts = obj.r*sin(t) + obj.y; end function obj = move(obj) obj.x = obj.x + obj.vx; if((obj.x-obj.r)<=obj.minx | (obj.x+obj.r)>=obj.maxx); obj.vx = -1*obj.vx; if(obj.x<0) obj.x = -9.99+obj.r; else obj.x = 9.99-obj.r; end end obj.y = obj.y + obj.vy; if((obj.y-obj.r)<=obj.miny | (obj.y + obj.r)>=obj.maxy); obj.vy = -1*obj.vy; if(obj.y<0) obj.y = -9.99+obj.r; else obj.y = 9.99-obj.r; end end end end end %redball2.m clear clear clc for j = 1:40 v(j) = redball; end while(1) hold on; for j = 1:40 v(j).move; [xp yp] = v(j).getpts; plot(xp,yp,'ro'); axis([-10 10 -10 10]) end pause(.01) hold off; plot([0],[0]); end
From: John D'Errico on 10 Jul 2010 06:30 "Bob felleo" <bobbyfelleo(a)yahoo.com> wrote in message <i19fio$1to$1(a)fred.mathworks.com>... > i have a program that shows floating redballs i need a matlab program that would include gravity in this if you could change it to be the apportion answer i would be very great full > > %redball.m > classdef redball < handle .... Sigh. That would involve significantly rewriting your code. But it is your homework assignment to do after all. Perhaps you should think about how gravity acts. Or perhaps you should look at how the several codes were written on the file exchange that model a ball that falls under the force of a gravitational field. Finally, I will point out that in real life when you write code, it is a good thing to use comments! Code that has no comments explaining what each block of code does can be virtually impossible to decode. In fact, my recommendation is a comment for EVERY line of code. At the least, provide a comment for every code fragment, perhaps every block of 3-5 lines. Explain the purpose of that code fragment to follow, how it works. This way, when you get run over by a bus, and your colleagues are forced to maintain your legacy of code, they will not curse your bones every time they are forced to look at your code. John
|
Pages: 1 Prev: remove duplicate rows in sparse matrix Next: identification toolbox transfer function |