Hey there! Sign in to join this conversationNew here? Join for free
    • Community Assistant
    • Welcome Squad
    • Thread Starter
    Online

    20
    ReputationRep:
    Community Assistant
    Welcome Squad
    Okay so I'm not the best when it comes to MATLAB but I've got 3 questions to do and while I'm okay with understanding them, I do not understand how to make them work on MATLAB in a single .m script.

    For example, I have done question 1 which is:
    Spoiler:
    Show

    -


    and I've got the following code for it which works when run:
    Spoiler:
    Show


    -



    Now when it comes to question 2:
    -
    I just cannot seem to make it work!

    I have tried saying:
    Spoiler:
    Show



    -




    After the main function which outputs the graphs for Q1, but when I try to do u(1,1) as an example, it just says that "Undefined function or variable 'u'." when I've clearly defined it?

    I have also tried making this a part of the main function such that the code for Q2a goes between the main function and its end point, I still get the same error so I have absolutely no idea where I need to put this or how I need to structure it

    So any help on how to make Q2 work in this single .m file would be appreciated!
    • Community Assistant
    Offline

    13
    ReputationRep:
    Community Assistant
    (Original post by RDKGames)
    ...
    If your function u is called in the first function then having the code for u directly after the code for the first function is fine. u can still be called by the first function.

    However, you cannot call u itself in the command window as the file name u.m does not exist in your directory. If you want to call u itself then you will need it in a separate file.

    Also, I am not sure why you are creating the function u. You can define the array u without using a function.
    • Community Assistant
    • Welcome Squad
    • Thread Starter
    Online

    20
    ReputationRep:
    Community Assistant
    Welcome Squad
    (Original post by SherlockHolmes)
    If your function u is called in the first function then having the code for u directly after the code for the first function is fine. u can still be called by the first function.

    However, you cannot call u itself in the command window as the file name u.m does not exist in your directory. If you want to call u itself then you will need it in a separate file.

    Also, I am not sure why you are creating the function u. You can define the array u without using a function.
    Ah I see. Thank you for the help.

    However now I run into the issue where my graph is blank and I cannot see where I went wrong lol... By entering it into the command window it only gives a single value for u and not an array, can you point out where the issue is? Thanks.
    Spoiler:
    Show

    x=-12.5:0.05:12.5;
    T=0;
    u=24*(6+8*cosh(4*x-16*T)+cosh(8*x-128*T))/(6*cosh(2*x-56*T)+cosh(6*x-72*T)).^2;
    figure
    plot(x,u);
    • Community Assistant
    Offline

    13
    ReputationRep:
    Community Assistant
    (Original post by RDKGames)
    ...
    Personally, I would do it like this:

    Code:
    x = -12.5:0.5:12.5;
    T = 0;
    
    numberofpoints = length(x);
    u = NaN(1,numberofpoints); 
    
    for i = 1:numberofpoints
    u(i) = 24*(6+8*cosh(4*x-16*T)+cosh(8*x-128*T))/(6*cosh(2*x-56*T)+cosh(6*x-72*T)).^2;
    end
    • Community Assistant
    • Welcome Squad
    • Thread Starter
    Online

    20
    ReputationRep:
    Community Assistant
    Welcome Squad
    (Original post by SherlockHolmes)
    Personally, I would do it like this:

    Code:
    x = -12.5:0.5:12.5;
    T = 0;
    
    numberofpoints = length(x);
    u = NaN(1,numberofpoints); 
    
    for i = 1:numberofpoints
    u(i) = 24*(6+8*cosh(4*x-16*T)+cosh(8*x-128*T))/(6*cosh(2*x-56*T)+cosh(6*x-72*T)).^2;
    end
    Hi, I'm trying to answer this question, but I cannot seem to get the right code for the array for v_3 and it plots many functions at once, would you be able to point out where I'm going wrong?
    Spoiler:
    Show











    My code:


    Code:
    function test
    t = 0:0.01:5;
    
    for j=1:1 :length(t)
    
    for n=1:100
    v3(j,n)=(-1)^(n+1)*sin(n*t(j)*pi)/(n^2);
    end
    
    end
    
    plot(t,v3)
    end
    • Community Assistant
    Offline

    13
    ReputationRep:
    Community Assistant
    (Original post by RDKGames)
    ...
    Firstly, there should be a sum taking place in the for loop for n. You appear to be just assigning each value in the sum to a position in the array v3.

    From my understanding, the dimensions of the array v3 should be equal to the dimensions of the array t.

    For each value of t (hence a for loop for t), you want to calculate v_3(t) which is a sum (hence a for loop for n).
    • Community Assistant
    • Welcome Squad
    • Thread Starter
    Online

    20
    ReputationRep:
    Community Assistant
    Welcome Squad
    (Original post by SherlockHolmes)
    Firstly, there should be a sum taking place in the for loop for n. You appear to be just assigning each value in the sum to a position in the array v3.

    From my understanding, the dimensions of the array v3 should be equal to the dimensions of the array t.

    For each value of t (hence a for loop for t), you want to calculate v_3(t) which is a sum (hence a for loop for n).
    Thanks, I've got it
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Brexit voters: Do you stand by your vote?
    Useful resources

    Make your revision easier

    Maths

    Maths Forum posting guidelines

    Not sure where to post? Read the updated guidelines here

    Equations

    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
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • 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

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