Unable to plot output after function.
I gave my script below
syms theta
A = cos(theta).*[1 5 6; 2 9 3; 5 1 0];
B = tan(2*theta).*[8 5 1; 0 1 6; 2 4 7];
myfun = @(t,y)scriptname(t,y,A,B);
% dummy values for tspan and y0
tspan = [0 1];
y0 = [0; 0; 0];
% ode solver
sol = ode45(myfun,tspan,y0);
function dydt = scriptname(t,y,A,B)
V = [1; 5; 3];
% evaluation of A and B (numerical) with theta = y(3)
An = double(subs(A,y(3)));
Bn = double(subs(B,y(3)));
dydt = V-((An+Bn)*y);
end
for i=1.:3.
stator=y(:,i);
figure(1.);
subplot(2.,2.,i);
plot(t,stator);
xlabel('time');
ylabel('Current');
title(['stat current # ',int2str(i)]);
end
iam getting error like this
Function definitions in a script must appear at the end of the file.
Move all statements after the "scriptname" function definition to before the first local function definition.
How to resolve this??
Thanks in advance

 채택된 답변

KSSV
KSSV 2021년 9월 8일
편집: KSSV 2021년 9월 8일

0 개 추천

function myfun()
syms theta
A = cos(theta).*[1 5 6; 2 9 3; 5 1 0];
B = tan(2*theta).*[8 5 1; 0 1 6; 2 4 7];
myfun = @(t,y)scriptname(t,y,A,B);
% dummy values for tspan and y0
tspan = [0 1];
y0 = [0; 0; 0];
% ode solver
sol = ode45(myfun,tspan,y0);
%% Plot here
plot(sol.x,sol.y)
function dydt = scriptname(t,y,A,B)
V = [1; 5; 3];
% evaluation of A and B (numerical) with theta = y(3)
An = double(subs(A,y(3)));
Bn = double(subs(B,y(3)));
dydt = V-((An+Bn)*y);
Save the aboove code in myfun.m and run it.

댓글 수: 10

and then how to plot??
KSSV
KSSV 2021년 9월 8일
Check Sol, it will be a structure with required values. See it's fields and plot what you want.
Iam new to matlab.
i didnt get what your are suggesting.
I want the output waveforms after compiling script.
Can you explian clearly.
KSSV
KSSV 2021년 9월 8일
I have shown it in code already....check it.
After compiling your code(which you gave), it is giving this error
Unable to resolve the name Sol.x.
Error in Untitled5 (line 151)
plot(Sol.x,sol.y)
KSSV
KSSV 2021년 9월 8일
The variable is sol not sol (No capitlal S). Typo error. Edited the code.
Is there any way to plot without making entire script as function(means without using function myfun() at starting)???
syms theta
A = cos(theta).*[1 5 6; 2 9 3; 5 1 0];
B = tan(2*theta).*[8 5 1; 0 1 6; 2 4 7];
myfun = @(t,y)scriptname(t,y,A,B);
% dummy values for tspan and y0
tspan = [0 1];
y0 = [0; 0; 0];
% ode solver
sol = ode45(myfun,tspan,y0);
%% Plot here
plot(sol.x,sol.y)
Write the above in a min file. Save the below into a function.
function dydt = scriptname(t,y,A,B)
V = [1; 5; 3];
% evaluation of A and B (numerical) with theta = y(3)
An = double(subs(A,y(3)));
Bn = double(subs(B,y(3)));
dydt = V-((An+Bn)*y);
Now run the main file.
sorry to bothering you again, one last doubt.
i followed your 1st method(keeping whole script as function), i am getting all waveforms in one graph. How to plot each solution separatley.
figure
plot(sol.x,sol.y(1,:))
figure
plot(sol.x,sol.y(2,:))
figure
plot(sol.x,sol.y(3,:))

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

제품

질문:

2021년 9월 8일

댓글:

2021년 9월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by