Join TSR now and get all your revision questions answeredSign up now
    • Thread Starter

    It's a question on MatLab Integration program, but have a look at this code first. It's a part of the Integration on Rectangle Method program:

    [LINES 1-3]
    x = 1 : 2 : 5; % x=lower limit: strip width: upper limit
    % These are not supposed to be constants, but for this question's simplicity, let's keep it that way.

    [LINE 4]
    y_x = input ( 'Enter your function of x = ' )
    % allowing the user to modify the function as required

    [LINE 5]
    S = 2 .* ( sum (abs (y_x)) )

    Now, that works fine with the subsequent lines to calculate the approximation of the integration; but, I want what was input by the user for y_x to be also used for the plot TITLE, DISP, and others. The issue is y_x turns to an array instantly. If I turn the input into string [Ex: y_x = input ( ' Enter you function of x = ' , 's') ], it works with the DISP and TITLE, but it doesn't process the x array, so I don't get the integration approximation. Example, say x.^2 was input. Hence, y_x = x.^2

    [LINE 6 - 7]
    plot( x , y_x)
    title(['Numeric Integration for' , num2str(y_x) , 'with 2 slices')

    Instead of title being 'Numeric Integration for x.^2 with 2 slices', what shows is 'Numeric Integration for 1 9 25 with 2 slices.' [1 9 25] is the squares for x=1 : 2 : 5. That's because the input has turned to an array and info is lost. Anyone knows how to get around this problem?

    mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm chemeng
    • Thread Starter

    LOL no wonder I thought the reply was pretty quick

    I did a bit of googling and found this:

    x = 1 : 2 : 5;
    y_x = input ('Enter your function of x = ','s');
    y_eval = eval(y_x);
    S = 2 .* sum(abs(y_eval))
    plot( x , y_eval)
    title(['Numeric Integration for ',num2str(y_x),' with 2 slices'])
    You can do something like the above. Its a simple change once you use the eval() function.
Should MenACWY vaccination be compulsory at uni?
Useful resources

Make your revision easier


Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here


How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

Thinking about a maths degree?

Chat with other maths applicants

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.