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.

 채택된 답변

madhan ravi
madhan ravi 2018년 12월 9일
편집: madhan ravi 2018년 12월 9일
you‘re running a function file without an input which causes error

댓글 수: 10

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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2018년 12월 9일
편집: Stephen23 2018년 12월 9일
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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2018년 12월 9일

편집:

2018년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by