Newbie: How To Write Xn+1 = Functions? - - MathWorks

Có thể bạn quan tâm

Skip to content
  • MATLAB Answers
  • File Exchange
  • Cody
  • AI Chat Playground
  • Discussions
  • Contests
  • Blogs
  • More
    • Communities
    • Treasure Hunt
    • Community Advisors
    • Virtual Badges
    • About
  • More

You are now following this question

  • You will see updates in your followed content feed.
  • You may receive emails, depending on your communication preferences.
Newbie: How to write Xn+1 = functions? Follow 49 views (last 30 days) Show older comments Vic Zommer Vic Zommer on 15 Jun 2016
  • Vote0
  • Link

    https://www.mathworks.com/matlabcentral/answers/290166-newbie-how-to-write-xn-1-functions

    Cancel Copy to Clipboard
  • Vote0
  • Link

    https://www.mathworks.com/matlabcentral/answers/290166-newbie-how-to-write-xn-1-functions

    Cancel Copy to Clipboard
Answered: John D'Errico on 15 Jun 2016 Accepted Answer: John D'Errico Hi!! Can somebody be so nice to show me an example how to plot this function into Matlab or just Manuals/Beginners Guide where I can find help on Xn+1 functions. Sorry :( Xn+1 = An*XnAn={A0 if n is even; A1 if n is odd;X0>0A1, A2 > 0Or if it is easier for you: Xn+1=r*Xn/1+XnX-1, X0 > 0r>0
0 Comments Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

John D'Errico John D'Errico on 15 Jun 2016
  • Vote1
  • Link

    https://www.mathworks.com/matlabcentral/answers/290166-newbie-how-to-write-xn-1-functions#answer_225651

    Cancel Copy to Clipboard
  • Vote1
  • Link

    https://www.mathworks.com/matlabcentral/answers/290166-newbie-how-to-write-xn-1-functions#answer_225651

    Cancel Copy to Clipboard
Open in MATLAB Online I'd normally not answer a basic question. But you did show some thought.A loop is simplest. Yes, there are lots of ways you could do this in a more sophisticated way. But a loop is a good way to start for something simple.% picking some simple values for N, A0, A1N = 100;A0 = 1.5;A1 = 0.75;% Preallocate X to be the right size. DO THIS FOR VECTORS% that will grow in size. If not you will be unhappy one day!!!!!!!!X = zeros(1,N);% I'll assume the first element is A1X(1) = A1;for n = 1:(N-1)if rem(n,2) == 0% even case An = A0;else% odd case An = A1;end% step to increment X(n) to X(n+1) X(n+1) = An*X(n)endBe careful. You cannot have an X(0) element. MATLAB indexing is 1 based.Ok, so, COULD we have done this in a vectorized way? Of course. In fact, that is what you should strive for as you grow in ability.N = 100;A0 = 1.5;A1 = 0.75;A = [A1,A0];X = cumprod(A(rem(1:N,2) + 1));Faster to write the second way, and I'll bet it is faster to execute.
0 Comments Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

Mathematics and Optimization Symbolic Math Toolbox MuPAD Advanced Software Development Code Performance Find more on Code Performance in Help Center and File Exchange

Tags

  • discreet
  • math
  • set

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Close MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • (English)
  • (Deutsch)
  • (Français)
  • (简体中文)
  • (English)

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Từ khóa » Xn+1