John Rose
John Rose

Reputation: 23

Matlab Function with Varying parameters

I need help figuring out how to code the following problem. Any help would be greatly appreciated!

Create a function that will take a vector/array input for x (1 by n) and a scalar input for a, and produce the output defined by the following equation:

y(x,a)=((xsin(ax-2))/(sqrt(1+(ax)^2)  
-π ≤ x ≤ π  
a={.5 1 1.5 2}  

The equation must be vectorized in terms of x and the output from the function is the array y which has the same dimension as the array x.

Write a script that calls this function to compute y(x,a) for the range of x defined above and each value of the parameter a. Results should be stored in a solution matrix using a different row of the solution matrix for each value of a.

So far for my function I have:

function [y] = part1(a,x)
y=((x*sin(a*x-2))/(sqrt(1+(a*x).^2)));
end

I'm not sure how to output this into the solution matrix

For my script I have:

%%
clear,clc

a={0.5 1 1.5 2};
x=-pi:0.1:pi;

for
    part1(x,a)
end

I'm getting the following errors when I run this now:

Undefined function 'mtimes' for input arguments of type 'cell'.

Error in part1 (line 4)
    y=((x*sin(a*x-2))/(sqrt(1+(a*x).^2)));

Error in labtest2 (line 8)
    y(i,:)=part1(x,a(i));

EDIT

I've made some changes and am still getting some errors that I cannot resolve.

Here is my full code for function followed by full code for script:

Function

function [y] = part1(x,a)

nx=numel(x);
na=numel(a);

y=((x.*sin(a.*x-2))./(sqrt(1+(a.*x).^2)));
size(y)=[nx na]
end

Script

%%
clear,clc

a={0.5 1 1.5 2};
x=-pi:0.1:pi;

for i = 1:length(a)
  y(i,:)=part1(x,a(i));
end

Errors

Undefined function 'times' for input arguments of type 'cell'.

Error in part1 (line 6)
    y=((x.*sin(a.*x-2))./(sqrt(1+(a.*x).^2)));

Error in labtest2 (line 8)
    y(i,:)=part1(x,a(i));

Upvotes: 2

Views: 1109

Answers (3)

John Rose
John Rose

Reputation: 23

Finally got the whole thing worked out.

Function

function [y] = part1(x,a)

    y=((x.*sin(a.*x - 2))./(sqrt(1 + (a.*x).^2)));

end

Script

%%
clear all;
clc;
close all;

x=[-pi:.1:pi];
a=[.5:.5:2];

for i=1:length(a)
   y(i,:)=part1(x,a(i));

  plot(x,y)
end

Sol=[y]  

Upvotes: 0

Yossarian
Yossarian

Reputation: 5471

The reason you're getting Undefined function 'times' for input arguments of type 'cell' is because your variable a is a cell array. You need to change your assignment of a from

a={0.5 1 1.5 2};

to

a=[0.5 1 1.5 2];

which will make it just a normal array. Alternatively, you need to reference it with cell array notation: a{i} instead of a(i).

Upvotes: 2

Dang Khoa
Dang Khoa

Reputation: 5823

You're almost there. Note that you've written

function [y] = part1(a,x)

but you call it in your script as

part1(x,a)

so you should probably correct that.

A few things jump out at me:

  • You never assign the output of part1(x,a) to anything. You're told that

    Results should be stored in a solution matrix using a different row of the solution matrix for each value of a.

What I take this to mean is that the 1st row corresponds to part1() evaluated for the 1st element of a. Since we're operating on x which is a vector, that row will have multiple columns. Your output is indeed a matrix. In your case, length(-pi:0.1:pi) == 63, therefore size(y) == [4 63], where y is your output matrix.

  • Your for loop is backwards. You're told to accept scalar a and vector x. Therefore, your script should be something like:

    a = 0.5:0.5:2;
    x = -pi:0.1:pi;
    for i = 1:length(a)
        y(i,:) = part1(x, a(i));
    end
    

Note the use of length and the : operator. I want to iterate between 1 to length(a) (in this case, length(a) == 4) so that I can use the current a(i) value as an index into my output matrix, y. The : operator in y(i,:) signifies "The ith row and all columns of y will take the value output by part1(x,a(i))."

  • Your function needs to be changed up for element-by-element operations. Notice that, for instance, x*sin(a*x-2) works for scalar x but not vectors. This is because x is a vector and sin(a*x-2) is also a vector (since the sin call will operate element-by-element and a is a scalar). Trying to multiply two vectors together will result in errors since MATLAB will try to perform a matrix multiplication. Resolve this by replacing * with .*. This way it is unambiguous that you are going to multiply these two vectors element-by-element. You'll also need to change / to ./.

On another note, thank you for attempting to do your homework before asking SO for help. We've been getting a huge influx of questions from students that have made no attempt to do their own work before dumping it on us, so it's refreshing that we regulars of the MATLAB tag get to actual help out instead of telling people to do their own work.

Upvotes: 0

Related Questions