MATLAB Answers

Euler function where you can define equation, starting x and y values, number of steps and the step length

2 views (last 30 days)
Heine Rugland
Heine Rugland on 26 Mar 2020
Commented: Heine Rugland on 26 Mar 2020
This is what I have thus far
function [xverd, tilnaer] = Euler_met (f,x0,y0,no_steg,lengde)
g = @(x,y) f;
t = x0:0.01:x0+no_steg;
y = zeros(1,length(t));
y(1) = y0;
fprintf('\n x y ')
for s = 1:length(t)-1
fprintf('\n%4.3f %4.3f ',t,y)
y(s+1) = y(s)+lengde*g(t(s),y(s));
But when I try to run it with these values:
I get this error:
Undefined operator '*' for input arguments of type 'function_handle'.
y(s+1) = y(s)+lengde*g(t(s),y(s));
I've tried just defining the equation f in line 3 in the code as such:
g = @(x,y) 8*cos(x+y);
And that works perfectly, so why can I replace the equation as seen in the code at the top?

Accepted Answer

Torsten on 26 Mar 2020
Use g = f or g = @(x,y) f(x,y) instead of g = @(x,y) f.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!