Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:16:54+00:00 2026-06-18T23:16:54+00:00

I want my Taylor method to be able to prompt for functions when executed.

  • 0

I want my Taylor method to be able to prompt for functions when executed. I tried using input(prompt) as described in MATLAB’s documentation. The problem is, it prompts me to enter the function every time it’s encountered in the code.

function taylorMethod(a, b, h, alpha, order)

    f = @(t, y) input('Enter f(t, y): ');
    fPrime = @(t, y) input('Enter f''(t, y): ');
    taylor2 = @(t, w) f(t, w) + h/2*fPrime(t, w);

    if order > 2
        f2Prime = @(t, y) input('Enter f''''(t, y): ');
        taylor3 = @(t, w) taylor2(t, w) + h^2/factorial(3)*f2Prime(t, w);
        if order == 4
            f3Prime = @(t, y) input('Enter f''''''(t, y): ');
            taylor4 = @(t, w) taylor3(t, w) + h^3/factorial(4)*f3Prime(t, w);
        end
    end

    function res = t(i)
        if i == a
            res = a;
            return;
        end
        res = h + t(i - 1);
    end

    idx = a;
    for i = a:h:b
        fprintf('i = %d; t_i = %.2f; w(i) = %.10f\n', idx, t(idx), w(idx));
        idx = idx + 1;
    end


    function res = w(i)
        j = i - 1;
        if i == a
            res = alpha;
            return;
        end
        if order == 2
            res = w(j) + h*taylor2(t(j), w(j));
        elseif order == 3
            res = w(j) + h*taylor3(t(j), w(j));
        elseif order == 4
            res = w(j) + h*taylor4(t(j), w(j));
        end
        return;
    end

end

I also tried to store the user input in a string like this:

fString = input('Enter f(t, y): ', 's');
fPrimeString = input('Enter f''(t, y): ', 's');
f = @(t, y) fString;
fPrime = @(t, y) fPrimeString;
taylor2 = @(t, w) f(t, w) + h/2*fPrime(t, w);

But I got an error:

Error using + Matrix dimensions must agree.

Error in taylorMethod>@(t,w)f(t,w)+h/2*fPrime(t,w) (line 13)
taylor2 = @(t, w) f(t, w) + h/2*fPrime(t, w);

Error in taylorMethod/w (line 47)
res = w(j) + h*taylor2(t(j), w(j));

Error in taylorMethod (line 35)
fprintf(‘i = %d; t_i = %.2f; w(i) = %.10f\n’, idx, t(idx),
w(idx));

The reason I’m not passing the functions in as arguments is because I want to use the same code for different orders… and different orders won’t have the same number of functions needed.

Is there a way that I can prompt the user once for the functions, and use the functions throughout without prompting again?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:16:55+00:00Added an answer on June 18, 2026 at 11:16 pm

    Rather than asking for user input, couldn’t you just take a cell array of functions? That solves the problem with different numbers of functions, and opens your code up to much more flexible kinds of uses.

    Your code might then look something like:

    function taylorMethod(a, b, h, alpha, order, funcs)
    
        f = funcs{1}; % first element is f(t, y)
        fPrime = funcs{2}; % f'(t, y)
        taylor2 = @(t, w) f(t, w) + h/2*fPrime(t, w);
    
        if order > 2
            f2Prime = funcs{3}; % f''(t, y)
            taylor3 = @(t, w) taylor2(t, w) + h^2/factorial(3)*f2Prime(t, w);
            if order == 4
                f3Prime = funcs{4}; % f'''(t, y)
                taylor4 = @(t, w) taylor3(t, w) + h^3/factorial(4)*f3Prime(t, w);
            end
        end
    
       % the rest is the same
    

    which you might call like

    taylorMethod(a, b, h, alpha, 3, {@(t, y) y^3, @(t, y) 3*y^2, @(t, y) 6*y})
    

    providing as many derivatives as necessary (or more; it doesn’t care).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like 1264$LP@$JOHN TAYLOR VS NJ Traffic$LP@$0$LP@$ . I want to
want to know why String behaves like value type while using ==. String s1
Want to be able to provide a search interface for a collection of objects
Is there a way, a method, to be able to effectively run unit tests
I am marshaling objects in Grails to JSON using the JSON.registerObjectMarshaller() method, which is
How can i draw a set of rectangles on a canvas Using DrawRect method?
I currently have created markers but I want to be able focus/show the marker's
I'm creating a mock data source that I want to be able to pass
I have expanded sin function into a Taylor series. Now I want to evaluate
Want to update page after jeditable (using gem on_the_spot) has been updated. Below works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.