Issue in plotting of output after function

5 ビュー (過去 30 日間)
Bathala Teja
Bathala Teja 2021 年 9 月 8 日
コメント済み: KSSV 2021 年 9 月 8 日
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 日
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 件のコメント
Bathala Teja
Bathala Teja 2021 年 9 月 8 日
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.
KSSV
KSSV 2021 年 9 月 8 日
figure
plot(sol.x,sol.y(1,:))
figure
plot(sol.x,sol.y(2,:))
figure
plot(sol.x,sol.y(3,:))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by