The Student Room Group

MATLAB question

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



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

Spoiler



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

I have tried saying:

Spoiler



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 :s-smilie:

So any help on how to make Q2 work in this single .m file would be appreciated! :smile:
(edited 7 years ago)
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.
(edited 7 years ago)
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

Original post by RDKGames
...


Personally, I would do it like this:



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

Original post by SherlockHolmes
Personally, I would do it like this:










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 v3v_3 and it plots many functions at once, would you be able to point out where I'm going wrong?

Spoiler


My 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



(edited 7 years ago)
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 v3(t)v_3(t) which is a sum (hence a for loop for n).
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 v3(t)v_3(t) which is a sum (hence a for loop for n).


Thanks, I've got it :smile:
I know this thread was a while ago but wondering if anyone could help me with certain codes on MATLAB? thanks
Reply 8
Original post by izzieileye
I know this thread was a while ago but wondering if anyone could help me with certain codes on MATLAB? thanks

Start a new thread and post your questions.
Original post by mqb2766
Start a new thread and post your questions.

Okay I have! Anyone interested in helping me with MATLAB here’s my thread ! Thank you ! https://www.thestudentroom.co.uk/showthread.php?t=6193122&p=85661296#post85661296

Latest