Input Function of (x,y) For Loop Error

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);

 Accepted Answer

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

2 Comments

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)

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!