I need help with Matlab

function [y] = hwk1prob1(x)
hwk1prob1= @(x)[tanh(2*x(1)-3*x(2)^2)+1;
exp(.3*x(1)+.4*x(2))-4.7];
y = fsolve( myhwk1prob1,x)
end
This is the code I have written for solving this problem yet I keep runnig into errors such as not enough input arguments, undefined function. I am really new to matlab so any help would be greatly appreciated.

回答(2 个)

The problem is that you named your parent function and your anonymous function the same. That’s going to cause problems.
Try this slightly edited version:
function [y] = hwk1prob1(x)
hwk1prob1fcn = @(x)[tanh(2*x(1)-3*x(2)^2)+1;
exp(.3*x(1)+.4*x(2))-4.7];
y = fsolve(hwk1prob1fcn,x);
end
x = [42; 42];
y = hwk1prob1(x) % Function Call

5 个评论

I tried using your code instead and it still came up with the same issues not enough input arguments, undefined function. So I am at a loss of words since I am just a novice with matlab and it is extremely frustrating to deal with.
The code I posted ran for me without error. I would not have posted it otherwise.
It produced:
y =
10.5195
-4.0207
The two lines at the bottom, x = [42; 42]; y = hwk1prob1(x); should go outside of the hwk1prob1.m file . They are showing how to execute the code.
However... your assignment is to create a hwk1prob1.m that does not expect any input, and which determines its own x value to pass as the second parameter to fsolve()
I just wanted to make sure there was nothing wrong with my matlab so the values came out just fine. Thanks for all your help.
If my Answer helped you solve your problem, pleae Accept it!

请先登录,再进行评论。

Walter Roberson
Walter Roberson 2019-2-10

0 个投票

You are asked to write a program hwk1prob1.m . It is not asked to be a function . No input will be passed to it.
Your code needs to try several different x values to find one that works.
Numerically, your function has solutions because of round-off error. Mathematically at least one of the two inputs needs to be infinite for there to be a solution. This is partly because the code shown for fun1 does not implement the equation in the description. Look at where the 1 is relative to the tanh() call: in the equation it is inside the tanh() but in the code it is outside the tanh() . This makes a large difference in the meaning of the equation.

2 个评论

If that is the case could you show me how to get started with matlab because I am just a beginner with what you said in mind because I am still confused?
x0 = SomethingThatYouWillHaveToFigureOut
fsolve(@fun1, x0)

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by