syntax error with using the ode45 function
1 view (last 30 days)
Show older comments
Eliraz Nahum
on 29 Sep 2018
Answered: Eliraz Nahum
on 29 Sep 2018
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
0 Comments
Accepted Answer
Walter Roberson
on 29 Sep 2018
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?
0 Comments
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!