i'm trying to input function of x,y
dy = input ('dy/dx = ','s');
dy = @(x,y) dy;
and also try
dy = input ('dy/dx = ','s');
dy = inline(dy);
then make a for loop to calculate y(2) based on the value of y(1) and dy(1) ===> which a function x,y
for o = 1 : 1 : n
y(o+1) = y(o) + delx * dy(x(o),y(o));
dy(o+1) = feval(dy,x,y);
end
here I want to use the just calculated y(2) to calculate the value of dy(2) and when run that code give me that error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Final (line 500)
y(o+1) = y(o) + delx * dy(x(o),y(o));
===================================================
update:
thank you (@Walter Roberson)
this help in convert the string to function
still there that error
Not enough input arguments.
when enter the for loop
for o = 1 : 1 : n
y(o+1) = y(o) + delx * dy(x(o),y(o));
dy(x(o+1),y(o+1)) = feval(dy(o),x(o),y(o));
end
and when made it
for o = 1 : 1 : n
y(o+1) = y(o) + delx * dy(x(o),y(o));
dy(o+1) = feval(dy,x,y);
end
the error is
Matrix dimensions must agree.
Error in Final>@(x,y)x-y
Error in Final (line 500)
dy(o+1) = feval(dy,x,y);

 채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 3일

1 개 추천

dy_string = input ('dy/dx = ','s');
dy = str2fun( ['@(x,y) ', dy_string] );

댓글 수: 2

thank you @Walter this help in convert the string to function
still there that error
Not enough input arguments.
when enter the for loop that when make the loop like that
for o = 1 : 1 : n
y(o+1) = y(o) + delx * dy(x(o),y(o));
dy(x(o+1),y(o+1)) = feval(dy(o),x(o),y(o));
end
and when made it
for o = 1 : 1 : n
y(o+1) = y(o) + delx * dy(x(o),y(o));
dy(o+1) = feval(dy,x,y);
end
the error was
Matrix dimensions must agree.
Error in Final>@(x,y)x-y
Error in Final (line 500)
dy(o+1) = feval(dy,x,y);
dy(x,y)
with no feval.
However, do not assign the output to dy(o+1) . dy is a function handle . If you need a record of the output of the function, use a different variable.
y(o+1) = y(o) + delx * dy(x(o), y(o));
dy_val(o+1) = dy(x(o), y(o+1));
However you need to think about whether the second dy calculation should use x(o) or x(o+1)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by