Can someone help me with this question? I keep getting the error "Not enough input arguments. Error in assesment2question1 (line 5) for m = 1:N"

The question I have is:
Consider the Maclaurin expansion: x/(1-x) = x + x^2 + x^3 + ...
Write a MATLAB function func(x,N) that returns the Maclaurin expansion using the first N terms. Use the function to plot, func(x,2), func(x,4) and func(x,8) on the same axes, for -0.5<x<0.5.

 Respuesta aceptada

madhan ravi
madhan ravi el 9 de Dic. de 2018
Editada: madhan ravi el 9 de Dic. de 2018
you‘re running a function file without an input which causes error

10 comentarios

An input for N? Sorry im very new to matlab.
yes... upload your code and the function
Here is my code. Im not entierly sure this is right, but like I said, im new to matlab.
function [ y ] = func(x,N)
y = x;
for m = 1:N
ym = x.^m;
y=y+ym;
end
end
syms x
N=4; % example N
y = func(x,N);
fplot(y,[-0.5,0.5],'-or','Linewidth',2) % x from -0.5 to 0.5 %%function call
function y = func(x,N) % function definition
y(1) = x;
for m = 2:N
ym = x.^m;
y=y+ym;
end
end
For some reason when I try and run that I get "Function definitions are not permitted in this context."?
paste it in a script and run it , do not run it in command window!
I am running it in a script. I just get this.
hdrthdrh.png
ok so your using prior to 2016b version so save the function in a file with same name as a function and just run the code other than the function in command window.
Anytime :), if my answer solved your problem make sure to accept the answer.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 9 de Dic. de 2018
Editada: Stephen23 el 9 de Dic. de 2018
The simple solution using an anonymous function:
>> func = @(x,N) sum(bsxfun(@power,x,(1:N).'),1);
>> X = -0.5:0.1:0.5;
>> plot(X,func(X,2), X,func(X,4), X,func(X,8))
Giving:
Of course this would also be easy to write in a function file, if you wanted to:
function y = (x,N)
y = sum(bsxfun(@power,x,(1:N).'),1);
end
And just to make things clearer you can also add a legend, etc:
>> plot(X,func(X,2), X,func(X,4), X,func(X,8))
>> hold on
>> plot(X,X./(1-X),'*')
>> legend({'N=2','N=4','N=8','actual'})
func1.png

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Dic. de 2018

Editada:

el 9 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by