using ode45 with input arrays

2 次查看(过去 30 天)
Clarisa Williams
Clarisa Williams 2012-6-27
I am trying to use ode45 but I have three arrays and can't get it to work.
Here is my function:
function [ ydot] = integration( t )
%This function will integrate
theta=[2.05320782261561,2.05350842309907, etc. it is a 1x753 array gamma=[0.0919239472479973,0.0916551270720756,et. It is also a 1x753 array velocity=[157;165.432396880000;189.834767478843; etc. It is a 754x1 array
y(1)= velocity*cos(gamma)*cos(theta)
y(2)= velocity*sin(gamma)*sin(theta)
y(3)= velocity*sin(gamma)
end
Here is my script program:
tspan=[0 753];
yo=[0.710174472636493,4.98858733451279, 0]
[t,y]=ode45(@integration, tspan,yo)
size(t)
I tried this for if theta, gamma, and velocity equal a single number and it worked but I cannot get it to work in the array. I copied and pasted the arrays from .mat files, which I was given to work with.
  2 个评论
Richard Brown
Richard Brown 2012-6-28
编辑:Richard Brown 2012-6-28
Please format your code by selecting it and using the Code button.
(thanks Walter)
Jan
Jan 2012-6-28
What does "can't get it to work" explicitly mean? Do you get an error message? If so, which one? Does Matlab moan, that you want to reply "ydot" but define "y" only?

请先登录,再进行评论。

回答(1 个)

Richard Brown
Richard Brown 2012-6-28
编辑:Richard Brown 2012-6-28
OK, I understand what you're asking now
First make sure theta, gamma, and velocity are column vectors of the same length, and assuming the samples have unit spacing. Then
y = cumtrapz([velocity .* cos(gamma) .* cos(theta), ...
velocity .* sin(gamma) .* sin(theta), ...
velocity .* sin(theta)])
should do the trick. ode45 is not required as there is no dependence on y in your rhs.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by