5 Comments

function f = function_5(x)
a = 1;
sum1 = 0;
for i =1: size(x,1)
for j=1:25
sum1 = (1/(j + (x(i,1) - a)^6+(x(i,2) - a)^6));
end
f(i) = 0.002 + sum1;
end
end
My attempt. Would this be correct?
No, the equation involves which in MATLAB would be a(i,j)
hamza
hamza on 18 Oct 2022
Edited: Walter Roberson on 1 May 2023
Q: how can i run this code on matlab?
#include<iostream>
using namespace std;
int main() {
float A[100][100], t, x, B[100], z;
int R, C, k = 0, m = 0, l = 0, n = 0, p = 0;
cout << "Rows:";
cin >> R; //input rows in augmented matrix
cout << "Columns:";
cin >> C; //input columns in augmented matrix
for (int i = 0; i < R; i++) //augmented matrix input loop
{
for (int j = 0; j < C; j++) {
cin >> A[i][j];
}
}
cout << endl;
cout << "AUGMENTED MATRIX\n"; //Display the matrix entered
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
cout << endl;
while (k < R) {
if (A[k][k] == 0) //Row interchange if element of primary diagonal of coefficient matrix is zero
{
for (int i = k + 1; i < R; ++i) {
if (A[i][k] != 0) {
for (int j = 0; j < C; ++j) {
t = A[i][j];
A[i][j] = A[k][j];
A[k][j] = t;
}
++n;
break;
}
}
for (int j = 0; j < C; j++) {
if (A[k][j] != 0)
++l;
}
if (l == 0) //If all the elements of a row are 0 then there are infinite solutions
{
cout << "\nThere are infinitely many solutions of the system of equations.\n";
return 0;
} else if (l == 1 && A[k][C - 1] != 0) //If all the elements, except the last one, of a row are 0 then no solution exists
{
cout << "\nNo solution exits for the system system of equations.\n";
return 0;
}
l = 0;
z = A[k][C - 1] / A[k + 1][C - 1];
for (int j = k + 1; j < C; j++)
{
if ((A[k][j] / A[k + 1][j]) != z)
++p;
}
if (n == 0 && A[k][C - 1] != 0) {
if (p != 0) {
cout << "\nNo solution exists for the system of equations.\n";
return 0;
} else {
cout << "\nThe system of equations has infinite many solutions.\n";
return 0;
}
}
n = 0;
p = 0;
}
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
cout << endl;
x = A[k][k];
if (A[k][k] != 1) //Row transformation: Divide all the elements of the row by its first non-zero element from left side
{
for (int j = k; j < C; j++) {
A[k][j] /= x;
}
}
for (int i = k + 1; i <= R; i++) //Store values(in array B) of all the elements lying below(in same column as that element) the
{ //first non-zero element of that row(from left side).
B[i] = A[i][k];
}
for (int j = 0; j < C; j++) //Conversion of matrix to Echelon form
{
for (int i = k + 1; i <= R; i++) {
A[i][j] -= (A[k][j] * B[i]);
}
}
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
cout << endl;
++k;
}
while (m < (R - 1)) {
for (int i = 0; i < (R - m - 1); i++) //Conversion of matrix to Row Reduced Echelon form
{
B[i] = A[i][C - m - 2];
}
for (int j = 0; j < C; j++) {
for (int i = 0; i < (R - m - 1); i++) {
A[i][j] -= (A[R - m - 1][j] * B[i]);
}
}
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
cout << endl;
++m;
}
cout << "ROW REDUCED ECHELON FORM\n";
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "The solution to the system of equations is\n"; //Display solutions
for (int i = 0; i < R; i++) {
cout << "X" << i + 1 << " = " << A[i][C - 1];
cout << endl;
}
return 0;
}
ayman
ayman on 14 Nov 2023
hi
x(t)=
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Sign in to comment.

 Accepted Answer

Hi,
Following code illustrates the implementation of the above equation:
x=[2 3]; %x vector
f=function_5(x);
function f= function_5(x)
a=ones(2,25); % a(i,j)
sum1=0;
sum=0;
for j=1:25
for i=1:2
sum1= sum1 + (x(i) - a(i,j))^6;
end
sum = sum + 1/(j+sum1);
suom1=0;
end
f=0.002+sum;
end
Hope it helps!!

4 Comments

We recommend against providing complete solutions to homework problems.
This was his very first question, I didn't felt it's a bad idea to help someone.
We try to act more like tutors, ideally talking about what the challenges are with the approach a poster has taken, to guide them towards being able to find a solution themselves. This leads them to a greater understanding of programming and of MATLAB and of the topic they are working on -- which is the goal of taking courses. When we give guideance instead of finished solutions, the person can correct the work they themselves have done, and submit the result in good conscience that they did the work and understand the solution.
I’m sorry if I have caused any problems, I had done it on my own but was a little unsure about the equation conversion into Matlab. Thank you for your help. Also I’ll make sure not to ask for complete solutions in the future.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!