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

 Réponse acceptée

To get De you could use arrayfun:
a=1;
b=2;
ya=1;
m=40;
[t,y]=pure(@my,a,b,ya,m)
t = 41×1
1.0000 1.0250 1.0500 1.0750 1.1000 1.1250 1.1500 1.1750 1.2000 1.2250
y = 3.7363
[~,dE] = arrayfun(@(tv)my(tv,y),t)
dE = 41×1
1.0000 1.0506 1.1025 1.1556 1.2100 1.2656 1.3225 1.3806 1.4400 1.5006
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 commentaires

thank you so much for your answer
it worked....
there is thing when i try to obtain more than one output such as:-
[~,dE1,dE2,dE3,dE4] = arrayfun(@(tv)my(tv,y),t)
i got this error Not enough input arguments.
is that any thing i suppose to change for this case
many thanks .
Stephen23
Stephen23 le 16 Nov 2020
@Ali Najem : please post the complete error message. This means all of the red text.
Not enough input arguments.
Error in Run>@(tv)my(tv,Y1) (line 57)
[~,dEdw12,dEdb12,dEdw34,dEdb34]=arrayfun(@(tv)my(tv,Y1),T1);
Error in Run (line 57)
[~,dE1,dE2,dE3,dE4]=arrayfun(@(tv)my(tv,Y1),T1);
this is the error that i got if i need to call more than one outputs
Stephen23
Stephen23 le 17 Nov 2020
Modifié(e) : Stephen23 le 17 Nov 2020
@Ali Najem : please post the code of the function my that you are using, and the output of this command:
which my -all
This is it
function [dy1dt,dE1,dE2,dE3,dE4] =my(input,batches,m,w1,b1,w3,b3,y0,u,errortot4,errortot2,grad4,grad2)
for i = batches:batches+m-1
a1 = input(:,i);
z2 = w1*a1 + b1;
a2 = elu(z2);
z4 = w3*a2 + b3;
a4 = elu(z4);
e=(a4-y0(:,i));
error4 = e.*elup(z4);
errortot4 = errortot4 + error4;
grad4 = grad4 + error4*a2';
error2 = (w34'*error4).*elup(z2);
errortot2 = errortot2 + error2;
grad2 = grad2 + error2*a1';
end
dE1=grad2;
dE2=errortot2;
dE3=grad4;
dE4=errortot4;
w1=-u/m*dE1;
b1=-u/m*dE2;
w3=-u/m*dE3;
b3=-u/m*dE4;
dy1dt=[w1(:); b1(:); w3(:); b3(:)];
end
so, when i try the simple function like in my question i got dE correectly or even if i want more than one outputs now in this case same thing but i need to change my function.
i am also using:
[t,y]=pure(@my,a,b,ya,m)
now when i write this code
[~,dE1,dE2,dE3,dE4] = arrayfun(@(tv)my(tv,y),t)
when i run i got the error that i aforementioned
many thanks for helping me.
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.
still got error am trying diffrent example same thing happening with me but with diffrent error
G=9.8; L=2;
tspan=[0 2*pi];
z0=[pi/3 0];
f=(@(t,y)my(t,y,G,L));
[t,y]=ode15s(f,tspan,z0);
[~,dE] = arrayfun(@(tv)mine(tv,y,G,L),t)
and this is the file of my function
function [dydt,dE]=my(t,y,G,L)
dydt = [y(2) ; -G/L*sin(y(1))];
dE=G*10;
end
when i run i got this error:-
Error using arrayfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Error in run (line 18)
[r,dE] = arrayfun(@(tv)mine(tv,y,G,L),t)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by