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.

 Accepted Answer

you‘re running a function file without an input which causes error

10 Comments

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!
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.

Sign in to comment.

More Answers (1)

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

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!