I am trying to write code to accept 3 arbitrary functions of time (i.e. cos, sin, t^2, etc.) and pass them to a loop that solves for an approximate solution using Euler's Method (the for loop). The error I receive is "Nonscalar arrays of function handles are not allowed; use cell arrays instead." This is the line of code with the function handle F. The code is shown here:
% solve u'(t) = F(t,u(t)) where u = [u1(t), u2(t), u3(t)]'
% using forward euler method
% Initial conditions and setup
neqn = 3; % set a number of equations
h=input('Enter the step size: '); % step size will effect solution size
t=(0:h:4).';%(starting time value 0):h step size
nt = size(t,1); % size of time array
u = zeros(nt,neqn); % initialize the u vector with zeros
v=input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ');
u(1,:)= v; % the initial u value amd the first column
v1=input('Enter the first vector fuction of time u1(t): ','s');
v2=input('Enter the first vector fuction of time u2(t): ', 's');
v3=input('Enter the first vector fuction of time u3(t): ', 's');
u1 = str2func(v1);
u2 = str2func(v2);
u3 = str2func(v3);
F = @(t,u) [u1,u2,u3]; % define the function 'handle', F
% The loop to solve using Euler's Method
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end

답변 (2개)

Walter Roberson
Walter Roberson 2022년 4월 5일

0 개 추천

F = @(t,u) [u1(t,u), u2(t,u), u3(t,u)]; % define the function 'handle', F

댓글 수: 1

Walter, thanks for the tip. I revised the function handle as but still there is some 'sin' and not working.
v1=input('Enter the first vector fuction of time u1(t): ','s');
v2=input('Enter the first vector fuction of time u2(t): ', 's');
v3=input('Enter the first vector fuction of time u3(t): ', 's');
u1 = str2func(v1);
u2 = str2func(v2);
u3 = str2func(v3);
F = @(t,u) [u1(t,u1), u2(t,u2), u3(t,u3)]; % define the function 'handle', F
% F = str2func(['@(t,u) [',v1,',',v2,',',v3,']']);
>> Enter the step size: 0.1
Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: [0,0,0]
Enter the first vector fuction of time u1(t): sin
Enter the first vector fuction of time u2(t): cos
Enter the first vector fuction of time u3(t): tan
Undefined function 'sin' for input arguments of type 'function_handle'.

댓글을 달려면 로그인하십시오.

Torsten
Torsten 2022년 4월 5일

0 개 추천

% solve u'(t) = F(t,u(t)) where u = [u1(t), u2(t), u3(t)]'
% using forward euler method
% Initial conditions and setup
neqn = 3; % set a number of equations
h=input('Enter the step size: '); % step size will effect solution size
t=(0:h:4).';%(starting time value 0):h step size
nt = size(t,1); % size of time array
u = zeros(nt,neqn); % initialize the u vector with zeros
v=input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ');
u(1,:)= v; % the initial u value amd the first column
v1=input('Enter the first vector fuction of time u1(t): ','s');
v2=input('Enter the first vector fuction of time u2(t): ','s');
v3=input('Enter the first vector fuction of time u3(t): ','s');
F = str2func(['@(t,u) [',v1,',',v2,',',v3,']'])
% The loop to solve using Euler's Method
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end
plot(t,u)
end

댓글 수: 9

Chris Horne
Chris Horne 2022년 4월 5일
Now when I enter a function like sin(t), I get the error: "Warning: The input to STR2FUNC "sin(t)" is not a valid function name. This will generate an error in a future release.
> In eulertest2 (line 14)
Torsten
Torsten 2022년 4월 5일
편집: Torsten 2022년 4월 5일
I suggest googling the warning to find a better solution.
But who cares about future releases ...
Another way that works for me:
% solve u'(t) = F(t,u(t)) where u = [u1(t), u2(t), u3(t)]'
% using forward euler method
% Initial conditions and setup
neqn = 3; % set a number of equations
h=input('Enter the step size: '); % step size will effect solution size
t=(0:h:4).';%(starting time value 0):h step size
nt = size(t,1); % size of time array
u = zeros(nt,neqn); % initialize the u vector with zeros
v=input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ');
u(1,:)= v; % the initial u value amd the first column
v1=input('Enter the first vector fuction of time u1(t): ','s');
v2=input('Enter the first vector fuction of time u2(t): ','s');
v3=input('Enter the first vector fuction of time u3(t): ','s');
F = strcat('[',v1,',',v2,',',v3,']');
F = eval(['@(t,u)' F]);
% The loop to solve using Euler's Method
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end
plot(t,u)
Chris Horne
Chris Horne 2022년 4월 5일
Thanks. This worked!
Torsten
Torsten 2022년 4월 5일
편집: Torsten 2022년 4월 5일
And the other solution didn't work or only threw a warning ?
I can't test it at the moment.
Torsten
Torsten 2022년 4월 7일
Just out of curiosity:
Why do you use the Euler method to integrate a function that only depends on the independent variable t ?
There are methods that are much better suited and have a higher precision:
Chris Horne
Chris Horne 2022년 4월 7일
Thanks. Because I am learning Diff Eqs.
Torsten
Torsten 2022년 4월 7일
But these are no differential equations in the usual sense.
Chris Horne
Chris Horne 2022년 4월 7일
That's fine. Mathematics is broad and deep. Thanks for your help

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 4월 5일

댓글:

2022년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by