syntax error with using the ode45 function

1 次查看(过去 30 天)
hello,
I can't understand what I am doing wrong. the main code I am attaching is calling an external called "function". I added the external function as an additional file.
I am adding the error message as a photo.
pleas help!
thanks!!!
clear all
close all
clc
global M_rand index
n_iter=10; %number of iterations
var=1.5/100; %variation around the nominal value
M_rand=zeros(9,n_iter);
random_variables=zeros(9,n_iter);%columns of t1 w0 w11 t2 w12 w21 w22 T1 T2
random_variables_nom=[32.2,2797.64,1222.18,138.2,944.187,180.03,45.35,12221.82,1801.75]; %t1 w0 w11 t2 w12 w21 w22 T1 T2
random_variables_range=zeros(9,2);%columns of t1 w0 w11 t2 w12 w21 w22 T1 T2, while the first column in min and the second is max
for range_counter=1:1:9
random_variables_range(range_counter,1)=random_variables_nom(range_counter)*(1-var);
random_variables_range(range_counter,2)=random_variables_nom(range_counter)*(1+var);
end
for rand_counter=1:1:9
temp(rand_counter,:)=random_variables_range(rand_counter,1)+(random_variables_range(rand_counter,2)-random_variables_range(rand_counter,1))*rand(1,n_iter);
end
M_rand=temp;
g=9.8;
GM=0.039; %in MKS
q0=[0;(6.3782448*(10^6));10.585;60.03];
t_delta=0.01
t_span=[0:t_delta:150];
t=0;
F=@(t,q) [q(2);(-GM*(q(1)/(((q(1)^2)+q(3)^2)^1.5)))+(g*Functions(t_iter))*(q(2)/(((q(2)^2)+(q(4)^2))^0.5));q(4);(-GM*(q(3)/(((q(1)^2)+q(3)^2)^1.5)))+(g*Functions(t_iter))*(q(4)/(((q(2)^2)+(q(4)^2))^0.5))];
for counter_iter=1:1:n_iter
index=counter_iter;
for time=1:t_delta:length(t_span)
t_iter=time;
[t,q]=ode45(@(t,q) F(t,q),t_span,q0);
plot(t,q(1,:),'g')
hold on
plot(t,q(1,:),'g')
end
figure
end

采纳的回答

Walter Roberson
Walter Roberson 2018-9-29
You define
F=@(t,q) [q(2);(-GM*(q(1)/(((q(1)^2)+q(3)^2)^1.5)))+(g*Functions(t_iter))*(q(2)/(((q(2)^2)+(q(4)^2))^0.5));q(4);(-GM*(q(3)/(((q(1)^2)+q(3)^2)^1.5)))+(g*Functions(t_iter))*(q(4)/(((q(2)^2)+(q(4)^2))^0.5))];
at a time when you have not yet defined t_iter . To use the current value of t_iter you need to move the definition of F to inside your for loop.
Your graphics are wrong. You have
plot(t,q(1,:),'g')
hold on
plot(t,q(1,:),'g')
but q is a 2D array with the same number of rows that t has, with one column per output variable. Perhaps you want plot(t, q(:,1), 'g') . But why do you plot the same thing again?
You also have this plot inside a for loop, causing it to plot each time. But your output is nearly the same each time, so you cannot tell the difference between the lines.
You are looping over t_span but each time calling ode45 for the entire t_span . Why are you doing that? Are you working on a system that has two time dimensions, both of which just happen to be the same size?

更多回答(1 个)

Eliraz Nahum
Eliraz Nahum 2018-9-29
first of all, thank you for replying. by the way, I am trying to learn Matlab by myself and not an experienced user of it.
so first of all let's give you a link to my original problem (not answered yet) so it may clarify what my final intention is.here is a link: my original problem
I guess that my problem is the fact that I am calling to an external time dependent function while performing the ODE45 one. so that's why I am trying to create a variable called t_iter, so my external function will get the relevant time.
as for the plot - you are right, there is a mistake. I should write plot(q(:,1),q(:,3),'g') for seeing Y vs X.
please help, I am lost.

类别

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

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by