Hello,
I am very new to Matlab and I can't run that code with the following error: Error: The input character is not valid in MATLAB statements or expressions.
Here is the code (I didn't write it):
function err=bdf2(x0=1,h=0.1)
% formule de differences finies BDF2
diff = (3*f(x0)-4*f(x0-h)+f(x0-2*h))/(2*h);
err = abs(diff-df(x0));
fprintf(' x0 %e h %e erreur %e \n',x0,h,err)
% function f
function funct=f(x)
funct = sin(x);
% derivative f
function dfunct=df(x)
dfunct = cos(x);

2 Kommentare

Guillaume
Guillaume am 7 Jul. 2017
Bearbeitet: Guillaume am 7 Jul. 2017
We need the full text of the error message, particularly the bit that tells you which line is causing the error.
I've formatted your code as code, but please get rid of the useless blank lines you put in there and next time use the {}Code format button.
Thanks for you answer ! This is the full error message (_672bf7cd7e95dc88638c440f3215c7e2_bdf2 is the name of the file)
>> _672bf7cd7e95dc88638c440f3215c7e2_bdf2
_672bf7cd7e95dc88638c440f3215c7e2_bdf2
Error: The input character is not valid in MATLAB statements or expressions.
And sorry for the blank lines, I didn't see the Code format button :/

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Steven Lord
Steven Lord am 7 Jul. 2017

1 Stimme

function err=bdf2(x0=1,h=0.1)
That's not valid MATLAB syntax. If you want h to default to 0.1 and x0 to default to 1 you can do something like:
function err=bdf2(x0, h)
if nargin < 2
h = 0.1;
end
if nargin < 1
x0 = 1;
end

Weitere Antworten (2)

Geoff Hayes
Geoff Hayes am 7 Jul. 2017

1 Stimme

Guillaume - since your file has a function named bdf2 then your m-file must be named the same way as bdf2.m. You would then call it from the command line as
bdf2(42,43)
where you would pass in two value for x0 and h. (The 42 and 43 I'm passing in are just for illustration.)
I am surprised by the function signature though
function err=bdf2(x0=1,h=0.1)
I didn't realize that you could provide default values for your input parameters. Are you sure that this is valid?
Guillaume Fleurisson
Guillaume Fleurisson am 7 Jul. 2017

0 Stimmen

This is working, thank you all very much for your fast answers ! :)

Kategorien

Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by