How to extract output from function
56 ビュー (過去 30 日間)
古いコメントを表示
Hello, All
i have this functions file
function [dydt,dE]=my(x,y)
f=(x^2-y^2)*sin(x);
dE=x^2;
dydt=10+f;
end
and this my run file
a=1; b=2; ya=1; m=40;
f=(@(t,y)my(t,y));
[t,y]=pure(f,a,b,ya,m);
I need to obtain dE from the function how can i proceed this
thanks indeed..
this is pure function file
function [ts,ys] = pure(f,a,b,y0,N)
t0 = a; T = b;
h = (T-t0)/N;
ts = zeros(N+1,1);
ys = zeros(1,length(y0));
t = t0;
yold = y0;
ts(1) = t;
ys(1,:) = yold';
for i=1:N
s1 = f(t,yold);
ynew = yold + s1*h;
yold=ynew;
t = t + h;
ts(i+1) = t; ys(1,:) = yold';
end
end
0 件のコメント
採用された回答
Stephen23
2020 年 11 月 16 日
To get De you could use arrayfun:
a=1;
b=2;
ya=1;
m=40;
[t,y]=pure(@my,a,b,ya,m)
[~,dE] = arrayfun(@(tv)my(tv,y),t)
function [dydt,dE]=my(x,y)
f=(x^2-y^2)*sin(x);
dE=x^2;
dydt=10+f;
end
function [ts,ys] = pure(f,a,b,y0,N)
t0 = a; T = b;
h = (T-t0)/N;
ts = zeros(N+1,1);
ys = zeros(1,length(y0));
t = t0;
yold = y0;
ts(1) = t;
ys(1,:) = yold';
for i=1:N
s1 = f(t,yold);
ynew = yold + s1*h;
yold=ynew;
t = t + h;
ts(i+1) = t; ys(1,:) = yold';
end
end
7 件のコメント
Stephen23
2020 年 11 月 17 日
This is how you defined the function my, with thirteen input arguments:
function [dy1dt,dE1,dE2,dE3,dE4] =my(input,batches,m,w1,b1,w3,b3,y0,u,errortot4,errortot2,grad4,grad2)
This is how you call the function my, with two input arguments:
my(tv,y)
If the function requires thirteen input arguments, then you need to call it with thirteen input arguments. You need to provide all of its required inputs, not just the first two.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!